Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to save matrices in a file, effective way

Subject: How to save matrices in a file, effective way

From: Edwardo

Date: 24 Nov, 2012 23:57:06

Message: 1 of 5

Hi,

I a doing a program that multiplies a lot of matrices. All the matrices are stores in a cell matrix. where:

cell(index,1) is the matrix in the left
cell(index,2)is the matrix of the right
cell(index,3) is the result.

I want to save a lot of matrices in this way in a file (example):

cell(index,1) cell(index,2) = cell(index,3)
cell(index,1) cell(index,2) = cell(index,3)
cell(index,1) cell(index,2) = cell(index,3)

and so on.

I know that exist the function dlmwrite, but I dont know exactly how to use it so that I can have this format.

Help!
Thanks!

Subject: How to save matrices in a file, effective way

From: Bruno Luong

Date: 25 Nov, 2012 07:47:08

Message: 2 of 5

"Edwardo " <edwardo.rivera@upr.edu> wrote in message <k8rmsi$70c$1@newscl01ah.mathworks.com>...
> Hi,
>
> I a doing a program that multiplies a lot of matrices. All the matrices are stores in a cell matrix. where:
>
> cell(index,1) is the matrix in the left
> cell(index,2)is the matrix of the right
> cell(index,3) is the result.
>
> I want to save a lot of matrices in this way in a file (example):
>
> cell(index,1) cell(index,2) = cell(index,3)
> cell(index,1) cell(index,2) = cell(index,3)
> cell(index,1) cell(index,2) = cell(index,3)
>
> and so on.
>
> I know that exist the function dlmwrite, but I dont know exactly how to use it so that I can have this format.
>
> Help!

What's wrong with using SAVE and LOAD command ?

Bruno

Subject: How to save matrices in a file, effective way

From: Steven_Lord

Date: 27 Nov, 2012 16:47:48

Message: 3 of 5



"Edwardo " <edwardo.rivera@upr.edu> wrote in message
news:k8rmsi$70c$1@newscl01ah.mathworks.com...
> Hi,
>
> I a doing a program that multiplies a lot of matrices. All the matrices
> are stores in a cell matrix. where:
>
> cell(index,1) is the matrix in the left cell(index,2)is the matrix of the
> right
> cell(index,3) is the result.
> I want to save a lot of matrices in this way in a file (example):
>
> cell(index,1) cell(index,2) = cell(index,3)
> cell(index,1) cell(index,2) = cell(index,3)
> cell(index,1) cell(index,2) = cell(index,3)
> and so on.
>
> I know that exist the function dlmwrite, but I dont know exactly how to
> use it so that I can have this format.

If you want more control over the specific format with which your data is
written to the file, look at the FPRINTF function.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How to save matrices in a file, effective way

From: Edwardo

Date: 28 Nov, 2012 00:31:10

Message: 4 of 5

"Steven_Lord" <slord@mathworks.com> wrote in message <k92qrk$1eu$1@newscl01ah.mathworks.com>...
>
>
> "Edwardo " <edwardo.rivera@upr.edu> wrote in message
> news:k8rmsi$70c$1@newscl01ah.mathworks.com...
> > Hi,
> >
> > I a doing a program that multiplies a lot of matrices. All the matrices
> > are stores in a cell matrix. where:
> >
> > cell(index,1) is the matrix in the left cell(index,2)is the matrix of the
> > right
> > cell(index,3) is the result.
> > I want to save a lot of matrices in this way in a file (example):
> >
> > cell(index,1) cell(index,2) = cell(index,3)
> > cell(index,1) cell(index,2) = cell(index,3)
> > cell(index,1) cell(index,2) = cell(index,3)
> > and so on.
> >
> > I know that exist the function dlmwrite, but I dont know exactly how to
> > use it so that I can have this format.
>
> If you want more control over the specific format with which your data is
> written to the file, look at the FPRINTF function.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

I tried this:
fid=fopen("somefile.txt","w")
for index=1:size(cell,1)
  fprintf(fid,"%i %i = %i\n",cell(index,1),cell(index,2),cell(index,3));
end
fclose(fid);

but first, I have to convert the matrix that was in the cell, to a matrix (cell2matrix). But the output to the file was bad. it print a matrix, in a line...

Subject: How to save matrices in a file, effective way

From: Steven_Lord

Date: 28 Nov, 2012 15:15:26

Message: 5 of 5



"Edwardo " <edwardo.rivera@upr.edu> wrote in message
news:k93m0e$edb$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <k92qrk$1eu$1@newscl01ah.mathworks.com>...
>>
>>
>> "Edwardo " <edwardo.rivera@upr.edu> wrote in message
>> news:k8rmsi$70c$1@newscl01ah.mathworks.com...
>> > Hi,
>> >
>> > I a doing a program that multiplies a lot of matrices. All the matrices
>> > are stores in a cell matrix. where:
>> >
>> > cell(index,1) is the matrix in the left cell(index,2)is the matrix of
>> > the right
>> > cell(index,3) is the result.
>> > I want to save a lot of matrices in this way in a file (example):
>> >
>> > cell(index,1) cell(index,2) = cell(index,3)
>> > cell(index,1) cell(index,2) = cell(index,3)
>> > cell(index,1) cell(index,2) = cell(index,3)
>> > and so on.
>> >
>> > I know that exist the function dlmwrite, but I dont know exactly how to
>> > use it so that I can have this format.
>>
>> If you want more control over the specific format with which your data is
>> written to the file, look at the FPRINTF function.
>>
>> --
>> Steve Lord
>> slord@mathworks.com
>> To contact Technical Support use the Contact Us link on
>> http://www.mathworks.com
>
> I tried this:
> fid=fopen("somefile.txt","w")

Either open the file in text mode or view the file in an editor other than
Notepad.

http://www.mathworks.com/help/matlab/ref/fopen.html

"To open files in text mode, attach the letter 't' to the permission, such
as 'rt' or 'wt+'. For better performance, do not use text mode. The
following applies on Windows systems, in text mode:

    Read operations that encounter a carriage return followed by a newline
character ('\r\n') remove the carriage return from the input.

    Write operations insert a carriage return before any newline character
in the output.

This additional processing is unnecessary for most cases. All MATLAB import
functions, and most text editors (including Microsoft Word and WordPad),
recognize both '\r\n' and '\n' as newline sequences. However, when you
create files for use in Microsoft Notepad, end each line with '\r\n'. For an
example, see fprintf."

> for index=1:size(cell,1)
> fprintf(fid,"%i %i = %i\n",cell(index,1),cell(index,2),cell(index,3));
> end
> fclose(fid);
>
> but first, I have to convert the matrix that was in the cell, to a matrix
> (cell2matrix). But the output to the file was bad. it print a matrix, in a
> line...

What happens when you open this file in the MATLAB Editor rather than
Notepad, as I suspect you're doing from your use of the phrase "in a line"
describing the file?

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us