Dimitris Boukosis
Dimitris Boukosis

Reputation: 127

Save output in file - 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.

Upvotes: 0

Views: 715

Answers (1)

Carlos Borau
Carlos Borau

Reputation: 1473

You can either export it directly to excel:

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

Note that you if your data has different length in each iteration, you can build the ranges dinamically (eg: a column of variable size):

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

Alternatively you can export your data to any .txt or .csv file (which can be later opened with 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);

You can read more about fprintf formats here.

Upvotes: 1

Related Questions