MATLAB Matlab beginner fprintf into .txt

  • Thread starter Thread starter hoheiho
  • Start date Start date
  • Tags Tags
    Beginner Matlab
Click For Summary
The discussion revolves around issues related to saving a matrix output to a text file in MATLAB. The user initially attempts to use the `fprintf` function to format and write matrix data, but encounters errors due to incorrect syntax and misunderstanding of function definitions. Suggestions include using the `save` command with the `-ascii` option for a simpler approach to save data without needing to manually format it. There is also advice to ensure the function definition is correctly set up to handle output arguments, as the user receives an error indicating "Too many output arguments." Additionally, participants recommend reviewing MATLAB documentation for proper function templating and usage of commands. The conversation highlights the importance of understanding MATLAB's syntax and leveraging built-in functions for efficiency.
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
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
6K
Replies
1
Views
2K
Replies
1
Views
2K