(quick) Matlab fprintf question

  • Context: MATLAB 
  • Thread starter Thread starter astropi
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB programming issue related to using the fprintf function to write output stored in a variable to a text file. Participants explore the correct usage of file identifiers and permissions when opening files for writing.

Discussion Character

  • Technical explanation

Main Points Raised

  • One participant describes their attempt to save output from a variable using fprintf but encounters an error related to the file identifier.
  • Another participant suggests specifying the file ID as the first parameter in the fprintf function.
  • A later reply emphasizes the need to specify file permissions when opening the file, recommending the use of 'wt' for writing text.
  • One participant expresses continued difficulty, reiterating the error message received from MATLAB and questioning the fopen statement.
  • Another participant proposes using 'w' in the fopen command to indicate the intention to create the file if it does not exist.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the exact solution, as some suggestions are made but not all are confirmed to work by those experiencing the issue.

Contextual Notes

There is uncertainty regarding the specific error messages and the state of the file prior to the fopen command, which may affect the outcomes discussed.

astropi
Messages
47
Reaction score
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!
 
Physics news on Phys.org
Try specifying the file ID as the first parameter.

fileID = fopen('x.txt');
fprintf(fileID,'%d\n',x);
 
uart said:
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?
 
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 .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:
astropi said:
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.
 
uart said:
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!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K