Matlab beginner fprintf into .txt

In summary, the individual is trying to output their matrix result into a text file using the fprintf command. They have also tried using the save command with the -ascii option, but have encountered an error. They are seeking help on how to properly use the fprintf command and how to resolve the error with the save command.
  • #1
hoheiho
47
0
Dear everyone,
i am trying to put my matrix result from my function 'matrix(1:20,1:5,'matrix.txt') in a text file like
y
x 1 2 3 4 5
1 3.33 4.34 23.21 23.12 0.00
2 2.42 2.42 13.42 34.52 2.23
until x=20,y=5

Code:
 fid = fopen('file_name','w');
fprintf(fid,'\n y\n');
fprintf(fid,'\n x 1  2  3  4  5\n');
for j=1:length(x);
    fprintf(fid,'%3d %s\t%20.2f %s\t%20.2f %s\t%20.2f\n %s\t%20.2f %s\t%20.2f',x,1,2,3,4,5);
end
fclose(fid);

This code case many error:(, i try to search the similar code in google but it looks like mind. How can i output my matrix result into a text file?
 
Physics news on Phys.org
  • #2
I haven't used the fprintf command much at all, but you can use the -ascii option with save:

save(filename,variable,'-ascii')

furthermore, you can append lines to an already existent ascii file with:

save(filename,variable,'-ascii','-append')
 
  • #3
thx for reply
sorry,im not quite understand what u mean?
 
  • #4
What Pythagorean is saying is that you do not need to write a low level save function, as you might in C, Java or other programming languages (like you're trying to do).

Instead, you just have to use the save command (as illustrated by Pythagorean above):
http://www.mathworks.com/help/techdoc/ref/save.html

As a side note, all of the MATLAB documentation is available online (at the site linked above), including Getting Started guides and Tutorials. You can also find help on a function within MATLAB by using the help command. For instance, typing the following will bring up the internal help documentation for the save command:
>> help save

Now, if you only want to extract a portion of the matrix (as you seem to be doing, you could try the following):

Code:
stuff_to_save=matrix(1:20, 1:5);
save('mySavedStuff.txt', stuff_to_save, '-ascii');

Note that the apostrophes (denoting text strings) are mandatory.
 
  • #5
Thank you for your help
My first line of function is required to be function matrix(1:20,1:5,file_name)
And now i try to put the code in the .m:
Code:
 matrix_test=matrix(1:20, 1:5);
 save('file_name.txt', matrix_test, '-ascii');
then i type matrix(1:20,1:5,matrix_testing) in command windows and it say
? Error using ==> matrix
Too many output arguments.

My output is same as what i defined 'matrix_test=matrix(1:20, 1:5);'. Is there any things i miss?
 
  • #6
show us your matrix.m file. You possibly don't have output arguments in the header?
 
  • #7
hoheiho said:
Thank you for your help
My first line of function is required to be function matrix(1:20,1:5,file_name)
And now i try to put the code in the .m:
Code:
 matrix_test=matrix(1:20, 1:5);
 save('file_name.txt', matrix_test, '-ascii');
then i type matrix(1:20,1:5,matrix_testing) in command windows and it say
? Error using ==> matrix
Too many output arguments.

My output is same as what i defined 'matrix_test=matrix(1:20, 1:5);'. Is there any things i miss?

Pythagorean said:
show us your matrix.m file. You possibly don't have output arguments in the header?

So, are you trying to write a function named matrix (which you should really, really consider renaming) which takes in a filename and a range of rows and columns? If so, I would reconsider your approach. Your function declaration looks more like a function call. I would also suggest passing in a subset of a matrix rather than trying to pass in a whole matrix and then an interval range.

As you say you're a beginner to MATLAB, please look over the documentation page for function templating:
http://www.mathworks.com/help/techdoc/ref/function.html
 
  • #8
Thanks for all of the reply

So, are you trying to write a function named matrix (which you should really, really consider renaming) which takes in a filename and a range of rows and columns?
Yes.

I have tried to use another method
Code:
o=fopen('*testing.txt','wt');
fprintf(o,'%8.2f %8.2f %8.2f %8.2f %8.2f\n',m')
fclose;

m=my output of matrix. In my code, i write it like m(j,i)=a/c. I try to put m(j,i) instead of m or m'. The error is still coming out "Too many output arguments." What I am doing in my code is like find out a and c then put in m(1,1). Then j+1 coz of for loop, find out a and c then put in m(2,1) and so on. Finally my matrix will be 20x5.I hope i make it clear now

Thanks
 

1. How do I use the fprintf function in Matlab to write data into a .txt file?

The fprintf function in Matlab is used to write formatted data into a file. To write data into a .txt file, you can use the following syntax:
fprintf(fileID, format, A)
where fileID is the file identifier, format specifies the format of the data, and A is the data that you want to write into the file.

2. Can I use fprintf to write data into an existing .txt file?

Yes, you can use the fprintf function to append data to an existing .txt file. To do this, you need to specify the file identifier of the existing file in the fileID parameter. This will add the new data to the end of the file without overwriting the existing data.

3. How do I create a new .txt file using fprintf in Matlab?

To create a new .txt file using fprintf in Matlab, you need to specify a file name and the 'w' permission in the fileID parameter. This will create a new .txt file with the specified name and allow you to write data into it using the fprintf function.

4. What is the difference between fprintf and fwrite in Matlab?

The fprintf function is used to write formatted data into a file, while the fwrite function is used to write binary data into a file. This means that fprintf is more suitable for writing text-based data into a .txt file, while fwrite is better for writing numerical or binary data into a file.

5. How can I read the data from a .txt file created using fprintf in Matlab?

To read the data from a .txt file created using fprintf in Matlab, you can use the fscanf function. This function reads data from a file based on the specified format and returns the data in a variable. Alternatively, you can use the textscan function to read and parse data from a .txt file into a cell array or a table.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
961
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
Back
Top