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 a 4-D matrix in a file?

Subject: How to save a 4-D matrix in a file?

From: Jess J

Date: 28 Dec, 2010 04:36:05

Message: 1 of 13

Hello,

I have generated a 4-D matrix... how do i save the data in a file so that i can read it again later?

For an example, this is the generated matrix:

temp_avai_rule(z,:,:,m)

How do i save this matrix in a file?

Thank you.

Regards,
Jess

Subject: How to save a 4-D matrix in a file?

From: Yumnam Kirani

Date: 28 Dec, 2010 04:47:06

Message: 2 of 13

"Jess J" wrote in message <ifbpfl$2o7$1@fred.mathworks.com>...
Save the four 2-d matrices consequtively. Or save the elements in the form of sequence of tuples of four elements. It depends on what you prefer.

Yumnam Kirani Singh
Tronglaobi Awang Leikai

> Hello,
>
> I have generated a 4-D matrix... how do i save the data in a file so that i can read it again later?
>
> For an example, this is the generated matrix:
>
> temp_avai_rule(z,:,:,m)
>
> How do i save this matrix in a file?
>
> Thank you.
>
> Regards,
> Jess

Subject: How to save a 4-D matrix in a file?

From: Jess J

Date: 28 Dec, 2010 04:50:20

Message: 3 of 13

"Jess J" wrote in message <ifbpfl$2o7$1@fred.mathworks.com>...
> Hello,
>
> I have generated a 4-D matrix... how do i save the data in a file so that i can read it again later?
>
> For an example, this is the generated matrix:
>
> temp_avai_rule(z,:,:,m)
>
> How do i save this matrix in a file?
>
> Thank you.
>
> Regards,
> Jess


To be more precise, the matrix is a (19x19x19x125) 4-D matrix...

Thanks.

Regards,
Jess

Subject: How to save a 4-D matrix in a file?

From: Walter Roberson

Date: 28 Dec, 2010 04:58:21

Message: 4 of 13

On 27/12/10 10:36 PM, Jess J wrote:

> I have generated a 4-D matrix... how do i save the data in a file so
> that i can read it again later?
>
> For an example, this is the generated matrix:
>
> temp_avai_rule(z,:,:,m)
>
> How do i save this matrix in a file?

If m is a scalar value, that would devolve to a 3D or less matrix.

If z is a scalar value, that would be a 4D matrix (at most) with a
leading dimension of 1; the easiest way to store that would be to
squeeze() it down to 3D and store that.

Assuming that z and m are both vectors so that the array is truly 4D,
the first thing to do would be to assign that indexed result to a
variable, so that you have an un-indexed 4D variable to write out.

Then, use save() to save the un-indexed variable to a .mat file. Do
*not* use the -ascii option of save(), as it will not preserve the 4D
structure.

If it is important to save it to text in a form that would be recognized
as 4D when it is read in, then you would probably be best off writing
out text of the form

   cat(4, cat(3, [1 2 3; 4 5 6], [7 8 9; 10 11 12]), cat(3, [13 14 15;16
17 18], [19 20 21; 22 23 24]))

Each cat(3 call builds up a 3D array, and cat(4 would combine those 3D
arrays in to a 4D array.

I do not know of any command at the moment that would be able to write
such an output without a loop. On the other hand, I haven't had a need
to write out 4D arrays as text.

Subject: How to save a 4-D matrix in a file?

From: ImageAnalyst

Date: 28 Dec, 2010 05:05:41

Message: 5 of 13

Am I missing something here? I don't see that Jess required his data
to be in text form. Why not just save it in a .mat file with the
save() function? (This would be if you only needed to read it back in
again with MATLAB, and not some other program.) Even if you have one
particular value for z and m, you might find it simple and convenient
just to save the whole damn thing out because it's not very big (only
171475 elements).

Subject: How to save a 4-D matrix in a file?

From: Walter Roberson

Date: 28 Dec, 2010 05:30:01

Message: 6 of 13

On 27/12/10 11:05 PM, ImageAnalyst wrote:
> Am I missing something here? I don't see that Jess required his data
> to be in text form. Why not just save it in a .mat file with the
> save() function?

Jess did not specify. I covered save() first and then discussed what had
to be done if it was to be saved as text.

Saving via save() is nothing different for a 4D array than any other
array, and so it seemed to me unlikely to raise a specific question.
Saving a 4D array as text, though, is something not at all obvious and
is the sort of thing that would be worth asking about. I figured that on
the balance of probabilities it would be better to discuss text saving
as well.

Subject: How to save a 4-D matrix in a file?

From: Jess J

Date: 28 Dec, 2010 07:22:04

Message: 7 of 13

Walter Roberson <roberson@hushmail.com> wrote in message <uBeSo.14568$lL1.3917@newsfe21.iad>...
> On 27/12/10 11:05 PM, ImageAnalyst wrote:
> > Am I missing something here? I don't see that Jess required his data
> > to be in text form. Why not just save it in a .mat file with the
> > save() function?
>
> Jess did not specify. I covered save() first and then discussed what had
> to be done if it was to be saved as text.
>
> Saving via save() is nothing different for a 4D array than any other
> array, and so it seemed to me unlikely to raise a specific question.
> Saving a 4D array as text, though, is something not at all obvious and
> is the sort of thing that would be worth asking about. I figured that on
> the balance of probabilities it would be better to discuss text saving
> as well.



Sorry, I might have not explain clearly on my problem... erm... i tried the save() but it shows this error:

Argument must contain a string.

hmm... im not sure how to explain my prob... so i cut out part of the matrix... The one shows below is the 3-D matrix before i multiply it with another matrix (125x19)

temp_avai_rule1(:,:,15) =

  Columns 1 through 9

    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0020 0.0625 0.5001
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0017 0.0559 0.4474
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0013 0.0401 0.3205
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0007 0.0230 0.1837
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0003 0.0105 0.0843
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0039 0.0310
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0011 0.0091
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0003 0.0021
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0004
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000

  Columns 10 through 18

    1.0000 0.5001 0.0625 0.0020 0.0000 0.0000 0.0000 0.0000 0.0000
    0.8947 0.4474 0.0559 0.0017 0.0000 0.0000 0.0000 0.0000 0.0000
    0.6408 0.3205 0.0401 0.0013 0.0000 0.0000 0.0000 0.0000 0.0000
    0.3674 0.1837 0.0230 0.0007 0.0000 0.0000 0.0000 0.0000 0.0000
    0.1686 0.0843 0.0105 0.0003 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0620 0.0310 0.0039 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0182 0.0091 0.0011 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0043 0.0021 0.0003 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0008 0.0004 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0001 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000

  Column 19

    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000

**I need to read this matrix in the later part of coding... multiply them with weight in order to obtain a result...

Thanks for helping me...

Regards,
Jess

Subject: How to save a 4-D matrix in a file?

From: Walter Roberson

Date: 28 Dec, 2010 07:37:43

Message: 8 of 13

On 28/12/10 1:22 AM, Jess J wrote:

> Sorry, I might have not explain clearly on my problem... erm... i tried
> the save() but it shows this error:
>
> Argument must contain a string.

We need to see your save() command.

If you were explicitly trying to save temp_avai_rule(z,:,:,m)
then you can't do that: you have to save an entire variable, not a slice
of it.

Subject: How to save a 4-D matrix in a file?

From: Yumnam Kirani

Date: 28 Dec, 2010 07:44:05

Message: 9 of 13

You must give the name of the variable and the name of the files to which you want to save your variable. See the help on save.

Yumnam Kirani Singh
Tronglaobi Awang Leikai

"Jess J" wrote in message <ifc36s$p0d$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <uBeSo.14568$lL1.3917@newsfe21.iad>...
> > On 27/12/10 11:05 PM, ImageAnalyst wrote:
> > > Am I missing something here? I don't see that Jess required his data
> > > to be in text form. Why not just save it in a .mat file with the
> > > save() function?
> >
> > Jess did not specify. I covered save() first and then discussed what had
> > to be done if it was to be saved as text.
> >
> > Saving via save() is nothing different for a 4D array than any other
> > array, and so it seemed to me unlikely to raise a specific question.
> > Saving a 4D array as text, though, is something not at all obvious and
> > is the sort of thing that would be worth asking about. I figured that on
> > the balance of probabilities it would be better to discuss text saving
> > as well.
>
>
>
> Sorry, I might have not explain clearly on my problem... erm... i tried the save() but it shows this error:
>
> Argument must contain a string.
>
> hmm... im not sure how to explain my prob... so i cut out part of the matrix... The one shows below is the 3-D matrix before i multiply it with another matrix (125x19)
>
> temp_avai_rule1(:,:,15) =
>
> Columns 1 through 9
>
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0020 0.0625 0.5001
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0017 0.0559 0.4474
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0013 0.0401 0.3205
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0007 0.0230 0.1837
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0003 0.0105 0.0843
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0039 0.0310
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0011 0.0091
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0003 0.0021
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0004
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
>
> Columns 10 through 18
>
> 1.0000 0.5001 0.0625 0.0020 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.8947 0.4474 0.0559 0.0017 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.6408 0.3205 0.0401 0.0013 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.3674 0.1837 0.0230 0.0007 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.1686 0.0843 0.0105 0.0003 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0620 0.0310 0.0039 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0182 0.0091 0.0011 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0043 0.0021 0.0003 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0008 0.0004 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0001 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
> 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
>
> Column 19
>
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
> 0.0000
>
> **I need to read this matrix in the later part of coding... multiply them with weight in order to obtain a result...
>
> Thanks for helping me...
>
> Regards,
> Jess

Subject: How to save a 4-D matrix in a file?

From: Jess J

Date: 28 Dec, 2010 08:07:05

Message: 10 of 13

Walter Roberson <roberson@hushmail.com> wrote in message <btgSo.66007$wf4.7672@newsfe05.iad>...
> On 28/12/10 1:22 AM, Jess J wrote:
>
> > Sorry, I might have not explain clearly on my problem... erm... i tried
> > the save() but it shows this error:
> >
> > Argument must contain a string.
>
> We need to see your save() command.
>
> If you were explicitly trying to save temp_avai_rule(z,:,:,m)
> then you can't do that: you have to save an entire variable, not a slice
> of it.


Ya, that's the mistake i make... i think i manage to save it using the save()... but bcz its a 4-D format so i cannot check what i saved is correct or not... issit possible that i can read those data I have saved?

Subject: How to save a 4-D matrix in a file?

From: Yumnam Kirani

Date: 28 Dec, 2010 12:30:05

Message: 11 of 13

You can read the values in the same way as you read in the 3d array using colon operator.

Yumnam Kirani Singh
Tronglaobi Awang Leikai

"Jess J" wrote in message <ifc5r9$mc6$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <btgSo.66007$wf4.7672@newsfe05.iad>...
> > On 28/12/10 1:22 AM, Jess J wrote:
> >
> > > Sorry, I might have not explain clearly on my problem... erm... i tried
> > > the save() but it shows this error:
> > >
> > > Argument must contain a string.
> >
> > We need to see your save() command.
> >
> > If you were explicitly trying to save temp_avai_rule(z,:,:,m)
> > then you can't do that: you have to save an entire variable, not a slice
> > of it.
>
>
> Ya, that's the mistake i make... i think i manage to save it using the save()... but bcz its a 4-D format so i cannot check what i saved is correct or not... issit possible that i can read those data I have saved?

Subject: How to save a 4-D matrix in a file?

From: ImageAnalyst

Date: 28 Dec, 2010 13:33:13

Message: 12 of 13

On Dec 28, 3:07 am, "Jess J" <jessie_...@yahoo.co.uk> wrote:
> Ya, that's the mistake i make... i think i manage to save it using the save()... but bcz its a 4-D format so i cannot check what i saved is correct or not... issit possible that i can read those data I have saved?
-----------------------------------------------------------
Use the load() function to read it back in. Subtract it from what you
saved to see if there are any differences. Why are you having doubts
that it was not saved correctly?

Subject: How to save a 4-D matrix in a file?

From: Jess J

Date: 29 Dec, 2010 05:22:04

Message: 13 of 13

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <48e484e9-91cc-45a3-99a5-792a16870179@n10g2000yqd.googlegroups.com>...
> On Dec 28, 3:07 am, "Jess J" <jessie_...@yahoo.co.uk> wrote:
> > Ya, that's the mistake i make... i think i manage to save it using the save()... but bcz its a 4-D format so i cannot check what i saved is correct or not... issit possible that i can read those data I have saved?
> -----------------------------------------------------------
> Use the load() function to read it back in. Subtract it from what you
> saved to see if there are any differences. Why are you having doubts
> that it was not saved correctly?



Hi, thank you everyone who is helping me... the save and load functions solved my problem... You guys make my day... thanks once again :)

Tags for this Thread

No tags are associated with 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