fprintf has been giving blank files

16 views (last 30 days)
Ry
Ry on 25 Mar 2014
Edited: dpb on 12 Apr 2014
I have been trying to use fprintf to save my outputs into a file that has all my variables in columns to plot graphs later. Here is a rough example of my code
For k=1:1441-30+1
k
a=wire1(k:k=29);
b=wire2(k:k+29);
c=wire3(k:k+29);
ainc=wire1(k+1:k+30);
binc=wire2(k+1:k+30);
cinc=wire3(k+1:k+30);
mA=mean(a)
mB=mean(b)
mC=mean(c)
coefAAinc=corrcoef(a,ainc)
coefBBinc=corrcoef(b,binc)
coefCCinc=corrcoef(c,cinc)
cAB=corrcoef(a,b)
cAC=corrcoef(a,c)
cBC=corrcoef(b,c)
vAAinc=var(a,ainc)
vBBinc=var(b,binc)
vCCinc=var(c,cinc)
vAB=var(a,b)
vAC=var(a,c)
vBC=var(b,c)
%%%various forms of fprintf go here
fprintf('date_place.txt', '%s',a,'%s',b,'%s',c, and so on')
end
*(another example that got close but failed)
fileID=fopen('date_place.txt')
fprintf('date_place.txt','w')
fclose('all')
It was my understanding that 'w', or write, is a permission to save them. I have also tried to use what you see above(or some combination usually with %d,\n in there too). But if I get any file on the computer it is blank. Can anyone give me a push in the right direction?
  1 Comment
dpb
dpb on 31 Mar 2014
You didn't change any of the things I pointed out that are wrong with your fprintf statement...
  1. No file handle
  2. Intermixing of format string and values to print
  3. Placing the fopen flag for permission in the fprintf and repeating the name instead of using the handle
Again I commend the documentation and examples to you andfor you to follow their lead explicitly.

Sign in to comment.

Answers (3)

dpb
dpb on 2 Apr 2014
Edited: dpb on 3 Apr 2014
Again, you're not paying attention (at least close attention) to the doc's...
Two problems--
1) Didn't save the file handle from the fopen for use in fprintf
2) you've repeated the file name in the fprintf statement yet again instead of using the file handle you should've saved.
Again, these details are all in the documentation examples as well as in the example I gave(+) in the first answer but you seem to ignore what both say... :(
(+) OK, my example in the first didn't actually open the file but I presumed that since in that first attempt you did return it (you called it fileID there) that you at least had that part under control.
Again, read the doc's for fopen and fprintf carefully and look at the details of the examples, they're important.
Note what happens if remove the trailing semicolon from your print statement at the command line --
>> m1=mean(rand(3,1)); % make up some data
>> fprintf('LabRat9.txt', '%f\n', m1)
LabRat9.txt>>
See that it does precisely what I described the first time -- since there is no valid file handle, fprintf will echo output to the screen (which you've suppressed w/ the ';' so it wasn't shown) and assumes the (now the first) argument is the format string. Now, since you put your file name in that position, you have have no locations in your format string for any output variables to be formatted into. But fprintf doesn't (and can't) know that wasn't what you intended so it does what it's asked to do in line with the defined behavior. The remaining two argument fields are interpreted as outputs; one a literal string and the other a variable.
This behavior is to echo to the output any text that isn't a formatting sequence filling in the the (missing) formatting sequence(s) with the corresponding variable(s) from the variable list. But, since there's no where to put them, they're ignored.
ADDENDUM:
Meanwhile, you've opened (and orphaned) the file 'LabRat9.txt' but since you didn't save the file handle you can't access it. Hence, there's now an empty file of that name in existence in the OS but it won't have any content as you've noted. The only way you can now close this file and reaccess it will be to use
fclose('all')
or use
fids=fopen('all')
to get a list of all open handles and work way thru that list to find the specific file associated with each (if more than one).
  1 Comment
Ry
Ry on 11 Apr 2014
http://people.bath.ac.uk/jchb20/xx10190/demofprintf.pdf I am trying to get my outputs to print in the form of "final touches" on page 4 of the link above. The nice columns under the variable name would make it easy to graph later. So here is my updated attempt...
lr=fopen('LabRat30.txt', 'w');
fprintf('\n, k, a\n, ainc\n, b\n, binc\n, c\n, cinc\n, m1, m2, m3, coefA, coefB, coefC, cab, cac, cbc, vAB, vAC, vBC, vAAinc, vBBinc, vCCinc \n') % I have also tried a(k), b(k) ect to replicate the example, but it didn't really change for the better. Also in this example the /n's are for readability in the txt file but are not what I want to stick with.
for k=1:1441-30+1;
k;
a=wire1(k:k+29);
b=wire2(k:k+29);
c=wire3(k:k+29);
ainc=wire1(k+1:k+30);
coefAAinc=corrcoef(a,ainc);, coefA=coefAAinc(2,1);
binc=wire2(k+1:k+30);
cinc=wire3(k+1:k+30);
coefBBinc=corrcoef(b,binc);, coefB=coefBBinc(2,1);
coefCCinc=corrcoef(c,cinc);, coefC=coefCCinc(2,1);
m1=mean(a);
m2=mean(b);
m3=mean(c);
cAB=corrcoef(a,b);
cab=cAB(2,1);
cAC=corrcoef(a,c);
cac=cAB(2,1);
cBC=corrcoef(b,c);
cbc=cAB(2,1);
vAB=var(a,b);
vAAinc=var(a,ainc);
vAC=var(a,c);
vBBinc=var(b,binc);
vBC=var(b,c);
vCCinc=var(c,cinc);
fprintf(lr, '%g\n %f\n %f\n %f\n %f\n %f\n %f\n %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f \n', k, a, ainc, b, binc, c, cinc, m1, m2, m3, coefA, coefB, coefC, cAB, cAC, cBC, vAB, vAC, vBC, vAAinc, vBBinc, vCCinc');
end
fclose(lr)
I know the answer is staring me in the face, but I am struggling to get it just right. I feel one problem is in the way I am trying to line up the %f\n terms in the 2nd and 3rd fprintf. Thank you for your patience.

Sign in to comment.


dpb
dpb on 26 Mar 2014
It was my understanding that 'w', or write, would take all of my variables and save them...
I've no idea what would have given you that idea; 'w' is only a flag to fopen that says you're subsequently going to write to the file represented by the file handle.
As all the examples for fprintf show, you've got to actually pass some variable to it for it to do anything other than print any constant text (if any) that happens to be in a format string.
fprintf('date_place.txt', '%s',a,'%s',b,'%s',c, and so on')
The above is also wrong--the first argument to fprintf is the file handle returned from fopen (or if omitted the output goes to standard output, namely the console). Subsequently, it expects a formatting string and then a list of output variables. Since your format string is just a file name and no numeric or character fields, it will echo that string to output and nothing else since there's nowhere for the subsequent arguments to be placed in a formatted string.
It should look something like --
fmt =['%f %f %f\n'];
fprintf(fid,fmt,a,b,c)
to print the three variables a, b, c to a line.
doc fopen
doc fprintf
doc dlmwrite % and friends
for more details. Look carefully at the examples; they're there for a reason.
  1 Comment
Ry
Ry on 2 Apr 2014
Alright, lets try this again.
fopen('LabRat9.txt', 'w');
for k=1:1441-30+1;
k;
a=wire1(k:k+29);
m1=mean(a)
fprintf('LabRat9.txt', '%f\n', m1);
end;
So, this code is trying to say, "Open up a file called lab rat 9, find the mean of 30 values of wire1 and print out this out put in fixed point notation and create a new line each time." I am still getting a blank document. Do you have any bright ideas?

Sign in to comment.


dpb
dpb on 11 Apr 2014
Well, again the example shows you how but you don't seem to pay any attention to it--look at the example format string again and compare to yours -- see the difference?
The example (format string only)
'%3d %8.2f %13.3e %13.3e\n'
Yours (partial, shows the problem...
'%g\n %f\n %f\n %f\n %f\n %f\n %f\n %f %f ...
Irrespective of all the line feeds you've not specified a width field...
  2 Comments
Ry
Ry on 11 Apr 2014
Alright I am fiddling with the field width specifier to line them up nicely. But I need the columns to line up under the VARIABLE NAME. How do
I get the list: a b c as my labels up top
a b c
and #'s here
in nice columns
will be comming soon
dpb
dpb on 11 Apr 2014
Edited: dpb on 12 Apr 2014
Same thing--use '%Ws' for the field width. C (and hence Matlab) will right-adjust by default so to get the first one in the middle of the first column you'll want it's width/2 to be at the midpoint of the column width. For example, if you have an 8-column field and a 4-character label you'll want
12345678 columns
NAME %6s format
123.4567 8.4f format
After that use the field width--the difference will be the same so the alignment will stay. Old Fortran coding forms were great for lining out fields for such purposes; sadly Matlab/C formatting strings are much more of a pita than Fortran FORMAT.
Although there is a glitch in string formats that I don't know whether is a bug in Matlab or a result of the C specification that makes it expected behavior. That is, if you use an array in a specified-width field as the output variable, the width is ignored and the output run together whereas individual terms are ok. See the following demo --
>> t=char('Lab 1','Label 2', 'name C'); % make a few random labels
>> sprintf(repmat('%12s',1,3),t.'),fprintf('\n')
ans =
Lab 1 Label 2name C
000000000111111111122222222223333333 % Column counts
123456789012345678901234567890123456
>> sprintf(repmat('%12s',1,3),t(1,:), t(2,:), t(3,:)),fprintf('\n')
ans =
Lab 1 Label 2 name C
000000000111111111122222222223333333 % Column counts
123456789012345678901234567890123456
The latter honors the spacing request while the former doesn't. To make this even more of pita, you can't write an implied DO in Matlab to get the array elements.
The thing that makes me think it's perhaps a Matlab implementation bug (of course, all the vectoriztion stuff is TMW-specific) is that it works as expected for numeric fields---
>> sprintf('%5.2f %8.3f %10.2f%4.1f',rand(1,4)),fprintf('\n')
ans =
0.81 0.906 0.13 0.9
One thing that can at least make your writing of format strings simpler since there is no repeat field is to get into the repmat habit for the repetitive parts--instead of your above
'... %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f \n'
write sotoo
fmt=[ ... repmat('%8.3f',1,15) '\n']);
Much easier to have a given count than trying to type in all of them individually. Why on earth the C-lizards didn't use the already-extant FORMAT-style such that one could write a repeat count and reversion and so on is beyond human understanding.

Sign in to comment.

Categories

Find more on Entering Commands in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!