writematrix can't save all the numbers in a variable

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.
I see, it works! In my previous relpy, I deleted the wrong (slice).
Thank you for the detailed explanation. I didn't realize the this is a scalar. The code is based on my college's work and modified by myself with google search. I'm still learning the basics of Matlab/programing languages. It took me half day yesterday to learn how to export the variable results and struggled with this issue and totay it takes 15 mins to solve the problem. Probably it's very easy for you but absolutly amazing to me! Thank you very much!
Sorry Stephen23, I don't see a bottom to accept your comment.

Sign in to comment.

 Accepted Answer

Voss
Voss on 13 Dec 2022
Edited: Voss on 13 Dec 2022
kidney_right_t1_Mean_ROI1(slice) is a scalar (i.e., a single number, a single element of kidney_right_t1_Mean_ROI1). That is what you are writing to the xlsx file.
I think you mean to write the entire kidney_right_t1_Mean_ROI1 instead:
writematrix(kidney_right_t1_Mean_ROI1,filename,'Sheet',2,'Range','D2:M2')

2 Comments

Thank you as well! Sorry I couldn't reply the comment on my phone.

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Asked:

on 13 Dec 2022

Commented:

on 13 Dec 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!