0

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.

4
  • So, the data can not be structured in a table. can be? Commented Jul 12, 2017 at 8:18
  • You can use fprintf to export to csv, which you can open in excel. Commented Jul 12, 2017 at 8:22
  • If I understand your question correctly, you want to monitor the output while it is created. I don't think you can use Excel then, as it will not update the file's contents unless you close it and open it again. You can do that with a simple text file though! Commented Jul 12, 2017 at 8:30
  • @OmG unfortunately not because I have to save some matrixes and some plots, how can I fprintf in a csv file @m7913d? Commented Jul 12, 2017 at 8:31

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

What inputs should I use to this code so matlab knows what to save? Thank you very much!
well, it depends on your data. Show me an example of what you print in each loop
I should print a dynamic series of vectors (4-6 vectors), a number (result) for each vector and a total Distance.
Next time write a real example (with numbers-strings etc), I have to assume too many things... In any case: fprintf(fi,'%2.2f \t %2.2f \t %2.2f \t \n',M'); % assuming that M is a matrix where each row is one of your vectors, each of them have 3 elements (columns) fprintf(fi,'%2.2f \n',result_number); %assuming result is a float fprintf(fi,'%2.2f \n',distance); %assuming distance is a float Note that \n jumps to new lines, you can change it by \t to add tabs instead. Here you have format info: es.mathworks.com/help/matlab/matlab_prog/…
Oh, I'm really sorry, but thanks it was really helpful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.