how to write a table 1*4 which is inside a for loop into excel?
5 ビュー (過去 30 日間)
古いコメントを表示
Hello, I have a table S of size 1*4 inside a for loop which iterates 45 times. I want to store the values in the table for each iteration in an excel sheet.I want the values stored in a new row as the loop iterates, yet i dont want the table heading for every iteration. To sum it up, i want the table heading in the first row of the excel sheet and the values of each iteration in the subsequent rows. So far i have this can someone please help me with this code? what am i doing wrong? what should i have done?
for ii=1:c3
e1= graycoprops(g1);
S=struct2table(e1); % 1*4 table
tableHeaders = {'Contrast','Correlation','Energy','Homogeneity' }; %header for the table
filename = 'laws1.xlsx';
sheet = 1;
xlRange = 'A1';
xlswrite(filename, tableHeaders, sheet, xlRange); %writing table headers into excel sheet
mainArrayToWrite = [S.Contrast.', S.Correlation', S.Energy.', S.Homogeneity.']; %table values
filename = 'laws1.xlsx';
sheet = 1;
x1Range='A2';
xlswrite(filename, mainArrayToWrite, sheet, xlRange); %writng the table values into excel sheet
end
Thankyou in advance.
0 件のコメント
回答 (1 件)
Akshat
2024 年 11 月 5 日 8:19
The issue with this piece of code is that you are setting the "tableHeaders" field in every iteration. This ends up having multiple headings instead of just one in the beginning of the excel sheet.
In addition to this, you are not computing the row index dynamically in each iteration. Dynamic computation of the index will ensure that the data is written to new rows.
Lastly, you should start writing data from row 2, as the first is reserved for the headers.
Incorporating these changes, the code which should work for you is as follows:
filename = 'laws1.xlsx';
tableHeaders = {'Contrast', 'Correlation', 'Energy', 'Homogeneity'};
xlswrite(filename, tableHeaders, 1, 'A1');
for ii = 1:c3
e1 = graycoprops(g1);
S = struct2table(e1); % 1*4 table
mainArrayToWrite = [S.Contrast, S.Correlation, S.Energy, S.Homogeneity];
% Start writing from the second row, so add 1 to ii
x1Range = sprintf('A%d', ii + 1);
xlswrite(filename, mainArrayToWrite, 1, x1Range);
end
Please run it at your end and feel free to follow up if you have any other queries.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Import from MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!