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:
write multidimensional matrix to text file

Subject: write multidimensional matrix to text file

From: Jonathan W Smith

Date: 17 Jan, 2011 19:59:05

Message: 1 of 6

Hello,

With MATLAB, does anyone know how to write a multi-dimensional matrix into an text file

By multi-dimensional, I mean C(:,:,M,h,bottom_top) where the first : is for all latitudes and the 2nd : is for all longitude, M
is for day, h is for hour, bottom_top is vertical levels
I tried the following with no success:


text_name = 'lightning.txt'

dlmwrite(text_name, C(:,:,M,h,bottom_top),'\t')

If there are 2 M dimensions, 4 h dimensions, and 4 bottom_top dimensions means that he text file should contain 32 matrices (arrays) with a line skipped in between each one.

Thanks
Jonathan

Subject: write multidimensional matrix to text file

From: Sean de

Date: 17 Jan, 2011 20:15:21

Message: 2 of 6

"Jonathan W Smith" wrote in message <ih2729$e91$1@fred.mathworks.com>...
> Hello,
>
> With MATLAB, does anyone know how to write a multi-dimensional matrix into an text file
>
> By multi-dimensional, I mean C(:,:,M,h,bottom_top) where the first : is for all latitudes and the 2nd : is for all longitude, M
> is for day, h is for hour, bottom_top is vertical levels
> I tried the following with no success:
>
>
> text_name = 'lightning.txt'
>
> dlmwrite(text_name, C(:,:,M,h,bottom_top),'\t')
>
> If there are 2 M dimensions, 4 h dimensions, and 4 bottom_top dimensions means that he text file should contain 32 matrices (arrays) with a line skipped in between each one.
>
> Thanks
> Jonathan

Write and read it as binary with:
doc fopen
doc fwrite
doc fread

Subject: write multidimensional matrix to text file

From: Think two, count blue.

Date: 17 Jan, 2011 20:19:01

Message: 3 of 6

On 11-01-17 01:59 PM, Jonathan W Smith wrote:

> With MATLAB, does anyone know how to write a multi-dimensional matrix into an
> text file

There is no method provided by Mathworks; you will have to program it yourself.

 > If there are 2 M dimensions, 4 h dimensions, and 4 bottom_top dimensions
 > means that he text file should contain 32 matrices (arrays) with a line
 > skipped in between each one.

There is no Mathworks provided support for reading such files as
multi-dimensional arrays; you would have to program the reading yourself.

 > By multi-dimensional, I mean C(:,:,M,h,bottom_top)

If I were designing the output format, I would want a break or some kind of
indicator each time the dimension changed, as otherwise the output would not
be distinguishable from that for a matrix of size C(:,:,M*h*bottom_top)

Subject: write multidimensional matrix to text file

From: Jonathan W Smith

Date: 17 Jan, 2011 21:10:21

Message: 4 of 6

"Think two, count blue." <roberson@hushmail.com> wrote in message <ih28aj$cdm$1@nrc-news.nrc.ca>...
> On 11-01-17 01:59 PM, Jonathan W Smith wrote:
>
> > With MATLAB, does anyone know how to write a multi-dimensional matrix into an
> > text file
>
> There is no method provided by Mathworks; you will have to program it yourself.
>
> > If there are 2 M dimensions, 4 h dimensions, and 4 bottom_top dimensions
> > means that he text file should contain 32 matrices (arrays) with a line
> > skipped in between each one.
>
> There is no Mathworks provided support for reading such files as
> multi-dimensional arrays; you would have to program the reading yourself.
>
> > By multi-dimensional, I mean C(:,:,M,h,bottom_top)
>
> If I were designing the output format, I would want a break or some kind of
> indicator each time the dimension changed, as otherwise the output would not
> be distinguishable from that for a matrix of size C(:,:,M*h*bottom_top)



I just created these multi-dimensional arrays in matlab. It seems like they may have to be read in a dimension at a time. Is that what you saying above?

Subject: write multidimensional matrix to text file

From: Sean de

Date: 17 Jan, 2011 21:19:05

Message: 5 of 6

"Jonathan W Smith" wrote in message <ih2b7t$634$1@fred.mathworks.com>...
> "Think two, count blue." <roberson@hushmail.com> wrote in message <ih28aj$cdm$1@nrc-news.nrc.ca>...
> > On 11-01-17 01:59 PM, Jonathan W Smith wrote:
> >
> > > With MATLAB, does anyone know how to write a multi-dimensional matrix into an
> > > text file
> >
> > There is no method provided by Mathworks; you will have to program it yourself.
> >
> > > If there are 2 M dimensions, 4 h dimensions, and 4 bottom_top dimensions
> > > means that he text file should contain 32 matrices (arrays) with a line
> > > skipped in between each one.
> >
> > There is no Mathworks provided support for reading such files as
> > multi-dimensional arrays; you would have to program the reading yourself.
> >
> > > By multi-dimensional, I mean C(:,:,M,h,bottom_top)
> >
> > If I were designing the output format, I would want a break or some kind of
> > indicator each time the dimension changed, as otherwise the output would not
> > be distinguishable from that for a matrix of size C(:,:,M*h*bottom_top)
>
>
>
> I just created these multi-dimensional arrays in matlab. It seems like they may have to be read in a dimension at a time. Is that what you saying above?

You just have to record the size, it's an input argument to FREAD. Or you RESHAPE after reading it in as a vector.

Subject: write multidimensional matrix to text file

From: Jonathan W Smith

Date: 18 Jan, 2011 12:45:24

Message: 6 of 6

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ih2bo9$9gl$1@fred.mathworks.com>...
> "Jonathan W Smith" wrote in message <ih2b7t$634$1@fred.mathworks.com>...
> > "Think two, count blue." <roberson@hushmail.com> wrote in message <ih28aj$cdm$1@nrc-news.nrc.ca>...
> > > On 11-01-17 01:59 PM, Jonathan W Smith wrote:
> > >
> > > > With MATLAB, does anyone know how to write a multi-dimensional matrix into an
> > > > text file
> > >
> > > There is no method provided by Mathworks; you will have to program it yourself.
> > >
> > > > If there are 2 M dimensions, 4 h dimensions, and 4 bottom_top dimensions
> > > > means that he text file should contain 32 matrices (arrays) with a line
> > > > skipped in between each one.
> > >
> > > There is no Mathworks provided support for reading such files as
> > > multi-dimensional arrays; you would have to program the reading yourself.
> > >
> > > > By multi-dimensional, I mean C(:,:,M,h,bottom_top)
> > >
> > > If I were designing the output format, I would want a break or some kind of
> > > indicator each time the dimension changed, as otherwise the output would not
> > > be distinguishable from that for a matrix of size C(:,:,M*h*bottom_top)
> >
> >
> >
> > I just created these multi-dimensional arrays in matlab. It seems like they may have to be read in a dimension at a time. Is that what you saying above?
>
> You just have to record the size, it's an input argument to FREAD. Or you RESHAPE after reading it in as a vector.





Hello again,

I was able to get those multi-dimensional matrices written to files by using the following commands:

fid = fopen('lightning.txt', 'w');
fprintf(fid,'%8.4f %8.4f %8.4f %8.4f\n',C)

fclose(fid);

However the elements in each dimension have the incorrect matrix address:.

If a dimension of the multi-dimensional matrix takes this form:

C(:,:,1)

0 3 0 7
0 6 2 9
0 0 1 4
0 0 0 0

It prints to the text file incorrectly in this form:

0 0 0 0
4 1 0 0
9 2 6 0
7 0 3 0

I tried to do a rot90(C,2), but rot90 can only be used on 2D matrices. See below

???Error using ==> rot90
A must be a 2-D matrix.

Error in ==> xlat_xlon_test at 70
fprintf(fid,'1382753164843508800000000000000000

Any thoughts????

Jonathan

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