MATLAB:使用while循环在Excel中保存多个表

我有下面的while循环,读取和分析图像,然后将结果保存在Excel表格中,保存在Excel工作表中。 我最初做了一个单一的图像/表的代码,然后意识到我需要开发它的n个图像。

我基本上希望结果被保存在同一个Excel工作表中,而不会覆盖,理想情况下这些表是由一个空行垂直分隔的。

这是我现在的努力:

while(1) ... %code code code ... message = sprintf('Do you want to save the results in an Excel worksheet?'); reply = questdlg(message,'Run Program?','OK','Cancel', 'OK'); if strcmpi(reply, 'Cancel') % User canceled so exit. return; end % Table creation. % code code code % Saving table to Excel T = table(Diameter,BandWidth,n1,n2,P1,P2,Damage,... 'RowNames',Band); filename = 'Results.xlsx'; writetable(T, filename, 'Sheet',1, 'Range', 'A1','WriteRowNames',true); % Create a while loop to save more experiments to the Excel worksheet. promptMessage = sprintf('Do you want to process another photo?'); button = questdlg(promptMessage, 'Continue', 'Continue', 'Cancel', 'Continue'); if strcmpi(button, 'Cancel') break; end end 

如果它可以帮助你得到一个想法,每个表是一个6×8。

在while循环之前,声明一个单元格数组以保存最终将写入Excel文件的表。

 cellArrayOfTableToBeWritten = {}; 

在循环之前,定义一个单元arrays,它将作为一个空行。

 rowWidth = 8; blankrow = repmat({''},1,rowWidth); 

你现在在哪里写表格,而是在底部将一行空白行写入单元格arrays。

 cellArrayOfTableToBeWritten = [cellArrayOfTableToBeWritten; T.Properties.VariableNames; table2cell(T); blankrow]; 

一旦你的while循环完成后,将组合的单元格数组写入文件作为excel文件。

 xlswrite(filename, cellArrayOfTableToBeWritten);