How to save multiple tables in seperate sheets in Excel
이전 댓글 표시
Hello - I am running multiple files in MATLAB and would like to save the outputs from regionprops into one large Excel file. With my current code, which is below, it will only save as seperate .txt files. Help is appreciatied - thank you!
figure,imshow(BW);
stats = regionprops(BW,'MajorAxisLength');
t = struct2table(stats);
path = 'C:\Users\Test' ;
filename = [path, filesep, 'GFP',num2str(k)] ;
writetable(t,filename);
end
k stands for k=1:length(Files)
댓글 수: 1
Benjamin Drewry
2019년 12월 2일
Victoria,
Just use the writetable function with the name-value pair 'Sheet', number.
i.e. writetable(t, 'fName.xlsx', 'Sheet', 4);
Thanks,
Ben
답변 (2개)
Christopher Wallace
2018년 7월 13일
Hi Victoria,
https://www.mathworks.com/help/matlab/ref/xlswrite.html
?
You should be able to do something along the lines of:
xlswrite(filename, data, 'Sheet1')
But since the xlswrite function only accepts two-dimensional numeric, character array, or string array, or, if each cell contains a single element, a cell array, you'll have to reformat your data before putting it in.
?
If you'd like to add a new sheet just change the name of the sheet in the xlswrite parameters. If it the sheet doesn't exist it will be created.
?
Best Regards,
Chris
댓글 수: 9
Victoria
2018년 7월 13일
Victoria
2018년 7월 13일
Christopher Wallace
2018년 7월 13일
How about:
xlswrite([path, '\test.xlsx'], C, int2str(k))
Use:
xlswrite(fullfile(path,'test.xlsx'), C, int2str(k))
By the way Victoria, there is no need to convert the table to a cell when you can specify the sheet to write to when using writetable:
writetable(fullfile(path,filename),'Sheet',k)
Victoria
2018년 7월 13일
Christopher Wallace
2018년 7월 13일
편집: Christopher Wallace
2018년 7월 13일
Is 'MajorAxisLength' the only property you're wanting to record in the file? So you would have 'MajorAxisLength' in the top row followed by the data for each image in a new row below?
Consider taking the write function out of the for-loop and adding k to the stats variable
stats(k) = regionprops(BW,'MajorAxisLength');
then do the write once the for-loop is complete.
adding Paolo's answer in as well you would end up with something like:
figure,imshow(BW);
stats(k) = regionprops(BW,'MajorAxisLength');
t = struct2table(stats);
end
path = 'C:\Users\Test';
writetable(t,fullfile(path,filename));
Victoria
2018년 7월 13일
Christopher Wallace
2018년 7월 13일
Can you post the full code?
Peter Perkins
2018년 8월 3일
0 개 추천
Victoria, I really recommend that you not use xlswrite. As Paolo suggests, writetable can definitely write to sheets in an Excel file.
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!