writematrix can't save all the numbers in a variable
Show older comments
Hello, I tried to use writematrix to save my results in a variable but not fully succeeded.
I process a raw data at different positions, (1,2,3. etc) each time, and will get a result in corresponding column in the workspace for each postion. pealse see below:

I used the code below to save values but I can only save the latest one and overwrite the privious one. e.g., when I run postion 6, the 754.23 will overwrite the 1025.9 in the excel. then when I run positon 7, the 1006.2 will overwrite 754.23 in the excel. the values were only writen at D2 in the excel, not in the range I designed (D2:M2)


How to get all the values? i.e., from D2 to M2 in the excel, I get 0, 0, 0, 0, 1025.9, 754.23, 1006.2 ......
I read the writematrix tutorial, the M = magic(5) example works, but the method doesn't work for mine.
Thank you!
slice = 7; %position, everytime I change the number, I will get a new result in the same variable, at the corresponding column, e.g., column 7 in this case.
figure; imshow(Maps.kidney.t1(:,:,slice),[0 3000]); colormap('jet');
s = drawrectangle('coordination',[49.44415243 81.4586071 3 3]), wait(s);
mask = createMask(s);
kidney_right_mask(:,:,slice) = mask;
map = Maps.kidney.t1(:,:,slice);
val_mean = mean(map(mask==1))
kidney_right_t1_Mean_ROI1(slice) = val_mean
filename = 'C:\Users\Public\MRI\SZ_SD_1st\M1\Test_ROIs_Values.xlsx';
writematrix(kidney_right_t1_Mean_ROI1(slice),filename,'Sheet',2,'Range','D2:M2')% I only get results at D2, not from D2:M2.
3 Comments
Each time you call WRITEMATRIX you are providing it with a scalar number to save:
kidney_right_t1_Mean_ROI1(slice) % this is a scalar
and telling it to save that scalar value in a range D2:M2. Each time WRITEMATRIX tries its best by writing the scalar value in the first cell of that range (because absolutely nothing tells it to do otherwise, and it is a reasonable permissive behavior, i.e. very forgiving to non-precise users).
If you want to write a scalar value in different cell each time, then you will need to tell it precisely which cell you want to save that scalar value into.
Or ... the MATLAB approach: collect your output data into one array, and call WRITEMATRIX just once:
why not just save kidney_right_t1_Mean_ROI1 ? Much simpler.
Shuyang Zhang
on 13 Dec 2022
Edited: Shuyang Zhang
on 13 Dec 2022
Shuyang Zhang
on 13 Dec 2022
Accepted Answer
More Answers (0)
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!