COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### write multidimensional matrix to text file

• Email
• Follow

```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
```
 0
Reply Jonathan 1/17/2011 7:59:05 PM

See related articles to this posting

```"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
```
 0
Reply Sean 1/17/2011 8:15:21 PM

```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)
```
 0
Reply Think 1/17/2011 8:19:01 PM

```"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?
```
 0
Reply Jonathan 1/17/2011 9:10:21 PM

```"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.
```
 0
Reply Sean 1/17/2011 9:19:05 PM

```"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
```
 0
Reply Jonathan 1/18/2011 12:45:24 PM

5 Replies
686 Views

Similar Articles

12/2/2013 3:38:17 AM
page loaded in 4921 ms. (0)