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

2 次查看(过去 30 天)
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);

回答(2 个)

Joseph Cheng
Joseph Cheng 2014-3-21
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 个评论
dpb
dpb 2014-3-23
编辑:dpb 2014-3-23
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...

请先登录,再进行评论。


dpb
dpb 2014-3-22
编辑:dpb 2014-3-23
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!

Translated by