# Matlab beginner fprintf into .txt

• MATLAB
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?

Pythagorean
Gold Member
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')

sorry,im not quite understand what u mean?

MATLABdude
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.

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
Gold Member
show us your matrix.m file. You possibly don't have output arguments in the header?

MATLABdude
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?

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 im 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