(quick) Matlab fprintf question

  • MATLAB
  • Thread starter astropi
  • Start date
  • Tags
    Matlab
In summary, when you try to save a file in Matlab using the command "fileID = fopen('x.txt');", you get an error that says "Invalid file identifier. Use fopen to generate a valid file identifier." To save the file, you need to specify the permission, which is related to what you want to do with the file (read it, write to it, and so on).f
  • #1
47
0
I have output (stored in variable "x") that I want saved into a simple text file. If I use the command:

fprintf('%d\n',x);

it prints the data in a nice column. This is what I want. Then to save it to a file I try this:

fileID = fopen('x.txt');
fprintf('%d\n',x);
fclose(fileID);

however Matlab does not like this. When I searched the help file, it gave a similar example, so what am I doing wrong here? Thanks in advance!
 
  • #2
Try specifying the file ID as the first parameter.

fileID = fopen('x.txt');
fprintf(fileID,'%d\n',x);
 
  • #3
Try specifying the file ID as the first parameter.

fileID = fopen('x.txt');
fprintf(fileID,'%d\n',x);

Thanks for the help, but unfortunately it did not work. I still get this error from Matlab:

Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.


when I try my code:


fileID = fopen('x.txt');
fprintf('%d\n',x);
fclose(fileID);

the error I get is:

Error using fclose
Invalid file identifier. Use fopen to generate a valid file identifier.


I still can't figure out why?
 
  • #4
When you open the file, you need to specify the permission, which is related to what you want to do with the file (read it, write to it, and so on).

Here's a link to some documentation for fopen: http://amath.colorado.edu/computing/Matlab/OldTechDocs/ref/fopen.html [Broken].


Try this:
Code:
fileID = fopen('x.txt', 'wt');
fprintf('%d\n',x);
fclose(fileID);

I don't have MATLAB, so can't verify that this will work, but I think it will.
 
Last edited by a moderator:
  • #5
Thanks for the help, but unfortunately it did not work. I still get this error from Matlab:

Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.


when I try my code:


fileID = fopen('x.txt');
fprintf('%d\n',x);
fclose(fileID);

the error I get is:

Error using fclose
Invalid file identifier. Use fopen to generate a valid file identifier.


I still can't figure out why?
Ok, well in that case it appears that your problem is with the fopen statement (BTW. Is there any particular reason why you didn't post the actual error message in the first place!)

Try letting it know that the file doesn't currently exit and that you wish to create it.

fileID = fopen('x.txt','w');

This tells it that you want to open the file for writing.
 
  • #6
Ok, well in that case it appears that your problem is with the fopen statement (BTW. Is there any particular reason why you didn't post the actual error message in the first place!)

Try letting it know that the file doesn't currently exit and that you wish to create it.

fileID = fopen('x.txt','w');

This tells it that you want to open the file for writing.

Thanks! I really can't think of why I did not post the error in the first place, except that I've not had much sleep :)

Edit: OK, got it! Thanks everyone for the help!
 

Suggested for: (quick) Matlab fprintf question

Replies
4
Views
477
Replies
4
Views
899
Replies
6
Views
720
Replies
5
Views
1K
Replies
2
Views
1K
Replies
32
Views
1K
Replies
2
Views
963
Replies
2
Views
997
Replies
2
Views
919
Back
Top