Save output in file - matlab

时间:2017-07-12 08:09:39

标签: excel matlab

I have a code that i am using over and over n times in a for loop, so i keep fprintfing about 10 lines onto the command window for every one of the n runs through this for loop. Sometimes the output is too long for the command window. Could someone maybe tell me how to have this output continually be posted to an excel file? The one problem i anticipate is that since it is a for loop printing different results after each run though the loop, i would need the different output to keep posting on different lines of the excel file.

1 个答案:

答案 0 :(得分:1)

您可以将其直接导出到Excel:

excel_header={'header1','header2','header3'};
warning('off','MATLAB:xlswrite:AddSheet')
filename = 'testdata.xlsx';
xlswrite(filename,excel_header,'SheetName','A1:C1');

请注意,如果您的数据在每次迭代中具有不同的长度,则可以使用dinamically构建范围(例如:可变大小的列):

excel_range_dynamic=['A1:A' num2str(data_length)];

或者,您可以将数据导出到任何.txt或.csv文件(稍后可以使用excel打开):

%Save to txt file
fi=fopen('test.txt','w');
fprintf(fi,'%s \n',str1); %str1 is any string you have defined
fprintf(fi,'%s\n%s\n%s\n',str2{:}); %str2 is a cell containing several strings
fclose(fi);

您可以详细了解fprintf格式here