Matlab beginner fprintf into .txt

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

Discussion Overview

The discussion revolves around using MATLAB to output a matrix to a text file, specifically focusing on the use of the `fprintf` function and alternative methods for saving data. Participants are exploring issues related to function definitions, output arguments, and file handling in MATLAB.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant attempts to use `fprintf` to write a matrix to a text file but encounters errors, indicating confusion about the correct syntax and approach.
  • Another participant suggests using the `save` command with the `-ascii` option as a simpler alternative to `fprintf` for saving matrices to text files.
  • There is a discussion about the necessity of renaming the function `matrix` to avoid confusion with MATLAB's built-in functions, as well as the importance of correctly defining output arguments in function headers.
  • A participant shares an alternative method using `fopen` and `fprintf`, but continues to face the "Too many output arguments" error, indicating possible issues with how the function is structured or called.
  • Clarifications are requested regarding the structure of the user's function and the intended output, with suggestions to review MATLAB documentation for better understanding.

Areas of Agreement / Disagreement

Participants express differing views on the best method to output data to a text file, with some advocating for `fprintf` and others for `save`. There is no consensus on the optimal approach, and the discussion remains unresolved regarding the user's specific implementation issues.

Contextual Notes

Participants note potential limitations in the user's function definition and output handling, but do not resolve these issues. The discussion highlights the importance of understanding MATLAB's function structure and file I/O operations.

Who May Find This Useful

Beginners in MATLAB programming, particularly those interested in file handling and matrix operations, may find this discussion relevant.

hoheiho
Messages
43
Reaction score
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
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')
 
thx for reply
sorry,im not quite understand what u mean?
 
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.
 
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?
 
show us your matrix.m file. You possibly don't have output arguments in the header?
 
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
 
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
 

Similar threads

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