| New Reply |
(quick) Matlab fprintf question |
Share Thread |
| Aug23-12, 02:47 PM | #1 |
|
|
(quick) Matlab fprintf question
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! |
| Aug24-12, 04:56 AM | #2 |
|
Recognitions:
|
Try specifying the file ID as the first parameter.
fileID = fopen('x.txt'); fprintf(fileID,'%d\n',x); |
| Aug24-12, 12:31 PM | #3 |
|
|
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? |
| Aug24-12, 01:29 PM | #4 |
|
Mentor
|
(quick) Matlab fprintf question
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/...ref/fopen.html. Try this: Code:
fileID = fopen('x.txt', 'wt');
fprintf('%d\n',x);
fclose(fileID);
|
| Aug24-12, 01:30 PM | #5 |
|
Recognitions:
|
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. |
| Aug24-12, 01:46 PM | #6 |
|
|
Edit: OK, got it! Thanks everyone for the help!! |
| New Reply |
Similar discussions for: (quick) Matlab fprintf question
|
||||
| Thread | Forum | Replies | ||
| matlab beginner fprintf into .txt | Math & Science Software | 7 | ||
| CSE 1010 - MATLAB - User-Controlled Input and Output - fprintf | Engineering, Comp Sci, & Technology Homework | 0 | ||
| Matlab fprintf | Math & Science Software | 6 | ||
| MATLAB fprintf function | Engineering, Comp Sci, & Technology Homework | 3 | ||
| Quick MATLAB Question | Math & Science Software | 1 | ||