I need to print a string of text to a file. Then I need to put something in a for loop that will print values from a matrix

6 views (last 30 days)
It needs to be like this:
Iteration # MaxError Row Column 1 er(1) er(2) er(3) 2 er(1) er(2) er(3) 3 er(1) er(2) er(3) 4 er(1) er(2) er(3) 5 er(1) er(2) er(3)
the formatting isnt staying
i need a column with iteration number values will come from loop index i need a column with "max error" values will come from a spot in a matrix then two more columns essentially the same as the above one.
er is a 1x3 matrix im guessing i can get the iteration number from the for loop index please be as explcit as possible, I know almost nothing about MATLAB.
Thanks
this is what i have so far its giving me an error with fopen V is just a dummy variable im using to test. real one will also be a 1X3 matrix
fid = fopen('c:\\error.txt','wt'); V=[1 3 5] for x=1:10 fprintf(fid,'%f',x,V(1),V(2),V(3)); V=V*2 end
fclose(fid);
  1 Comment
dpb
dpb on 21 Mar 2014
Start w/
doc sprintf
Show your work to date and where you're having specific problems...doesn't help learn anything if don't put in at least some effort...

Sign in to comment.

Answers (2)

Joseph Cheng
Joseph Cheng on 21 Mar 2014
Does the fprintf work enough to get you your header? If you can get that and the rest of the output is just numbers i would suggest using dlmwrite.
for iteration=1:numberofiterations
tobewritten = [iteration er]; %build the row
dlmwrite([folderpath, filename],tobewritten,'-append'); %append the tobewritten data to the end of the file.
end
look at the documentation for dlmwrite but you'll still need to use fprintf to get your header and close the file before you dlmwrite. Hope that helps
  1 Comment
dpb
dpb on 23 Mar 2014
Edited: dpb on 23 Mar 2014
Actually, that's not a bad idea given the enhancements to dlmwrite to allow for append and the specific formatting. Too bad it can't also take an open file handle as an option as well as the file name excepting for having the open/close cycle every iteration may be some serious overhead for a large file. For a small one wouldn't be too bad probably...
To avoid that I'd suggest the prepending of the iteration/counter vector and write the array in "one swell foop" as demonstrated below...

Sign in to comment.


dpb
dpb on 22 Mar 2014
Edited: dpb on 23 Mar 2014
OK, good to post your try...do try to format it for legibility though--put a blank line and two blank spaces in front of the first code line. Look at the preview window until it looks legible...
OK, fair start...consider the following similar example--
V=rand(5,3); % some dummy data
fmt=[repmat('%8.3f',1,size(V,2)) '\n']; % fixed-width w/ right #/line
fprintf(fid,fmt,V.')
To write the header, use a counted '%s' field for each column to align them properly.
Read up on the use of formatting strings in detail in the doc for fprintf
ADDENDUM:
Forgot about you wanting the index row, too, sorry...
fmt=[repmat('%8.3f',1,size(V,2)+1) '\n']; % right #/line including counter
fprintf(fid,fmt,[(1:size(V,1)).' V].')
Just prepend the vector [1:size(V,1)].' to the array. NB: the .' on the overall to account for column-major storage order in Matlab. If you don't follow that, try it w/o the last ".'" transpose and see what happens instead.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!