writematrix can't save all the numbers in a variable

6 views (last 30 days)
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
Shuyang Zhang
Shuyang Zhang on 13 Dec 2022
Edited: Shuyang Zhang on 13 Dec 2022
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!
Shuyang Zhang
Shuyang Zhang on 13 Dec 2022
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')

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!