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:
strange behaviour of save

Subject: strange behaviour of save

From: Yuri Geshelin

Date: 29 Oct, 2008 19:17:02

Message: 1 of 7

Can anyone explain strange behaviour of save command?

I have two versions of an algorithm, which I execute as separate codes (functions code1.m and code2.m).
In the end, each function has these 2 lines, which save variables RCM8 and RCM11:

save RCM8 RCM8
save RCM11 RCM11

To verify that the results of both codes are identical, I execute these commands:

>> code1; RCM8_1 = load('RCM8'); RCM11_1 = load('RCM11');
>> code2; RCM8_2 = load('RCM8'); RCM11_2 = load('RCM11');
>> isidentical(RCM8_1,RCM8_2)

ans =

     1

>> isidentical(RCM11_1,RCM11_2)

ans =

     1

OK, this is fine with me. However, let me just save the variables in another way:

In the end of code1.m and code2.m, I write respectfully:

save all_RCMs1 RCM8 RCM11

save all_RCMs2 RCM8 RCM11

I expected that the two outputs would be identical. However,

>> code1; code2; isidentical(load('all_RCMs1'),load('all_RCMs2'))

ans =

     0

I investigated this and found that both codes still output the identical variables RCM8, RCM11 in files all_RCMs1.mat and all_RCMs2.mat.
The only difference is the order, in which these variables get written:

>> a1 = load('all_RCMs1')

a1 =

    RCM11: {8x2x5 cell}
     RCM8: {8x2x5 cell}

>> a2 = load('all_RCMs2')

a2 =

     RCM8: {8x2x5 cell}
    RCM11: {8x2x5 cell}

The question is, why does it happen? What does the order depend on? "help save" was not helpful.

(I know how to change the order of fields in a structure, but this is not the point).

Thanks,

Yuri

Subject: strange behaviour of save

From: Loren Shure

Date: 29 Oct, 2008 20:01:22

Message: 2 of 7

In article <geacre$ptv$1@fred.mathworks.com>, geshelin@hotmail.com
says...
> Can anyone explain strange behaviour of save command?
>
> I have two versions of an algorithm, which I execute as separate codes (functions code1.m and code2.m).
> In the end, each function has these 2 lines, which save variables RCM8 and RCM11:
>
> save RCM8 RCM8
> save RCM11 RCM11
>
> To verify that the results of both codes are identical, I execute these commands:
>
> >> code1; RCM8_1 = load('RCM8'); RCM11_1 = load('RCM11');
> >> code2; RCM8_2 = load('RCM8'); RCM11_2 = load('RCM11');
> >> isidentical(RCM8_1,RCM8_2)
>
> ans =
>
> 1
>
> >> isidentical(RCM11_1,RCM11_2)
>
> ans =
>
> 1
>
> OK, this is fine with me. However, let me just save the variables in another way:
>
> In the end of code1.m and code2.m, I write respectfully:
>
> save all_RCMs1 RCM8 RCM11
>
> save all_RCMs2 RCM8 RCM11
>
> I expected that the two outputs would be identical. However,
>
> >> code1; code2; isidentical(load('all_RCMs1'),load('all_RCMs2'))
>
> ans =
>
> 0
>
> I investigated this and found that both codes still output the identical variables RCM8, RCM11 in files all_RCMs1.mat and all_RCMs2.mat.
> The only difference is the order, in which these variables get written:
>
> >> a1 = load('all_RCMs1')
>
> a1 =
>
> RCM11: {8x2x5 cell}
> RCM8: {8x2x5 cell}
>
> >> a2 = load('all_RCMs2')
>
> a2 =
>
> RCM8: {8x2x5 cell}
> RCM11: {8x2x5 cell}
>
> The question is, why does it happen? What does the order depend on? "help save" was not helpful.
>
> (I know how to change the order of fields in a structure, but this is not the point).
>
> Thanks,
>
> Yuri
>
>


Try using isequal (documented) instead of isidentical (undocumented). I
believe isequal doesn't care about field order in structs (and
isidentical does). I realized that begs the question about the order
they are written.

--
Loren
http://blogs.mathworks.com/loren

Subject: strange behaviour of save

From: Yuri Geshelin

Date: 30 Oct, 2008 18:51:02

Message: 3 of 7

Loren Shure <loren@mathworks.com> wrote in message <MPG.23728b1c3b90f20c9898e3@news.mathworks.com>...
> In article <geacre$ptv$1@fred.mathworks.com>, geshelin@hotmail.com
> says...
> > Can anyone explain strange behaviour of save command?
> >
> > I have two versions of an algorithm, which I execute as separate codes (functions code1.m and code2.m).
> > In the end, each function has these 2 lines, which save variables RCM8 and RCM11:
> >
> > save RCM8 RCM8
> > save RCM11 RCM11
> >
> > To verify that the results of both codes are identical, I execute these commands:
> >
> > >> code1; RCM8_1 = load('RCM8'); RCM11_1 = load('RCM11');
> > >> code2; RCM8_2 = load('RCM8'); RCM11_2 = load('RCM11');
> > >> isidentical(RCM8_1,RCM8_2)
> >
> > ans =
> >
> > 1
> >
> > >> isidentical(RCM11_1,RCM11_2)
> >
> > ans =
> >
> > 1
> >
> > OK, this is fine with me. However, let me just save the variables in another way:
> >
> > In the end of code1.m and code2.m, I write respectfully:
> >
> > save all_RCMs1 RCM8 RCM11
> >
> > save all_RCMs2 RCM8 RCM11
> >
> > I expected that the two outputs would be identical. However,
> >
> > >> code1; code2; isidentical(load('all_RCMs1'),load('all_RCMs2'))
> >
> > ans =
> >
> > 0
> >
> > I investigated this and found that both codes still output the identical variables RCM8, RCM11 in files all_RCMs1.mat and all_RCMs2.mat.
> > The only difference is the order, in which these variables get written:
> >
> > >> a1 = load('all_RCMs1')
> >
> > a1 =
> >
> > RCM11: {8x2x5 cell}
> > RCM8: {8x2x5 cell}
> >
> > >> a2 = load('all_RCMs2')
> >
> > a2 =
> >
> > RCM8: {8x2x5 cell}
> > RCM11: {8x2x5 cell}
> >
> > The question is, why does it happen? What does the order depend on? "help save" was not helpful.
> >
> > (I know how to change the order of fields in a structure, but this is not the point).
> >
> > Thanks,
> >
> > Yuri
> >
> >
>
>
> Try using isequal (documented) instead of isidentical (undocumented). I
> believe isequal doesn't care about field order in structs (and
> isidentical does). I realized that begs the question about the order
> they are written.
>
> --
> Loren
> http://blogs.mathworks.com/loren

Thanks Loren,

OK, let's forget about isequal / isidentical. But the question remains. Here is a statement:

save all_RCMs RCM8 RCM11

In one case it saves variables RCM8 and RCM11 in such a way that

>> fnam = fieldnames(load('all_RCMs.mat'))

fnam =

    'RCM8'
    'RCM11'

In another scenario (with identical RCM8 and RCM11) THE SAME LINE generates file all_RCMs.mat such that fnam gets flipped:

>> fnam = fieldnames(load('all_RCMs.mat'))

fnam =

    'RCM11'
    'RCM8'

Why does it happen, what does the order depend on? I need to have control over this.

Thanks,

Yuri

Subject: strange behaviour of save

From: Loren Shure

Date: 31 Oct, 2008 13:32:05

Message: 4 of 7

In article <gecvmm$k79$1@fred.mathworks.com>, geshelin@hotmail.com
says...
> Loren Shure <loren@mathworks.com> wrote in message <MPG.23728b1c3b90f20c9898e3@news.mathworks.com>...
> > In article <geacre$ptv$1@fred.mathworks.com>, geshelin@hotmail.com
> > says...
> > > Can anyone explain strange behaviour of save command?
> > >
> > > I have two versions of an algorithm, which I execute as separate codes (functions code1.m and code2.m).
> > > In the end, each function has these 2 lines, which save variables RCM8 and RCM11:
> > >
> > > save RCM8 RCM8
> > > save RCM11 RCM11
> > >
> > > To verify that the results of both codes are identical, I execute these commands:
> > >
> > > >> code1; RCM8_1 = load('RCM8'); RCM11_1 = load('RCM11');
> > > >> code2; RCM8_2 = load('RCM8'); RCM11_2 = load('RCM11');
> > > >> isidentical(RCM8_1,RCM8_2)
> > >
> > > ans =
> > >
> > > 1
> > >
> > > >> isidentical(RCM11_1,RCM11_2)
> > >
> > > ans =
> > >
> > > 1
> > >
> > > OK, this is fine with me. However, let me just save the variables in another way:
> > >
> > > In the end of code1.m and code2.m, I write respectfully:
> > >
> > > save all_RCMs1 RCM8 RCM11
> > >
> > > save all_RCMs2 RCM8 RCM11
> > >
> > > I expected that the two outputs would be identical. However,
> > >
> > > >> code1; code2; isidentical(load('all_RCMs1'),load('all_RCMs2'))
> > >
> > > ans =
> > >
> > > 0
> > >
> > > I investigated this and found that both codes still output the identical variables RCM8, RCM11 in files all_RCMs1.mat and all_RCMs2.mat.
> > > The only difference is the order, in which these variables get written:
> > >
> > > >> a1 = load('all_RCMs1')
> > >
> > > a1 =
> > >
> > > RCM11: {8x2x5 cell}
> > > RCM8: {8x2x5 cell}
> > >
> > > >> a2 = load('all_RCMs2')
> > >
> > > a2 =
> > >
> > > RCM8: {8x2x5 cell}
> > > RCM11: {8x2x5 cell}
> > >
> > > The question is, why does it happen? What does the order depend on? "help save" was not helpful.
> > >
> > > (I know how to change the order of fields in a structure, but this is not the point).
> > >
> > > Thanks,
> > >
> > > Yuri
> > >
> > >
> >
> >
> > Try using isequal (documented) instead of isidentical (undocumented). I
> > believe isequal doesn't care about field order in structs (and
> > isidentical does). I realized that begs the question about the order
> > they are written.
> >
> > --
> > Loren
> > http://blogs.mathworks.com/loren
>
> Thanks Loren,
>
> OK, let's forget about isequal / isidentical. But the question remains. Here is a statement:
>
> save all_RCMs RCM8 RCM11
>
> In one case it saves variables RCM8 and RCM11 in such a way that
>
> >> fnam = fieldnames(load('all_RCMs.mat'))
>
> fnam =
>
> 'RCM8'
> 'RCM11'
>
> In another scenario (with identical RCM8 and RCM11) THE SAME LINE generates file all_RCMs.mat such that fnam gets flipped:
>
> >> fnam = fieldnames(load('all_RCMs.mat'))
>
> fnam =
>
> 'RCM11'
> 'RCM8'
>
> Why does it happen, what does the order depend on? I need to have control over this.
>
> Thanks,
>
> Yuri
>
>

Why do you need control over the order?

--
Loren
http://blogs.mathworks.com/loren

Subject: strange behaviour of save

From: Yuri Geshelin

Date: 31 Oct, 2008 15:07:01

Message: 5 of 7

Loren Shure <loren@mathworks.com> wrote in message <MPG.2374d2e32cda06bc9898e5@news.mathworks.com>...
> In article <gecvmm$k79$1@fred.mathworks.com>, geshelin@hotmail.com
> says...
> > Loren Shure <loren@mathworks.com> wrote in message <MPG.23728b1c3b90f20c9898e3@news.mathworks.com>...
> > > In article <geacre$ptv$1@fred.mathworks.com>, geshelin@hotmail.com
> > > says...
> > > > Can anyone explain strange behaviour of save command?
> > > >
> > > > I have two versions of an algorithm, which I execute as separate codes (functions code1.m and code2.m).
> > > > In the end, each function has these 2 lines, which save variables RCM8 and RCM11:
> > > >
> > > > save RCM8 RCM8
> > > > save RCM11 RCM11
> > > >
> > > > To verify that the results of both codes are identical, I execute these commands:
> > > >
> > > > >> code1; RCM8_1 = load('RCM8'); RCM11_1 = load('RCM11');
> > > > >> code2; RCM8_2 = load('RCM8'); RCM11_2 = load('RCM11');
> > > > >> isidentical(RCM8_1,RCM8_2)
> > > >
> > > > ans =
> > > >
> > > > 1
> > > >
> > > > >> isidentical(RCM11_1,RCM11_2)
> > > >
> > > > ans =
> > > >
> > > > 1
> > > >
> > > > OK, this is fine with me. However, let me just save the variables in another way:
> > > >
> > > > In the end of code1.m and code2.m, I write respectfully:
> > > >
> > > > save all_RCMs1 RCM8 RCM11
> > > >
> > > > save all_RCMs2 RCM8 RCM11
> > > >
> > > > I expected that the two outputs would be identical. However,
> > > >
> > > > >> code1; code2; isidentical(load('all_RCMs1'),load('all_RCMs2'))
> > > >
> > > > ans =
> > > >
> > > > 0
> > > >
> > > > I investigated this and found that both codes still output the identical variables RCM8, RCM11 in files all_RCMs1.mat and all_RCMs2.mat.
> > > > The only difference is the order, in which these variables get written:
> > > >
> > > > >> a1 = load('all_RCMs1')
> > > >
> > > > a1 =
> > > >
> > > > RCM11: {8x2x5 cell}
> > > > RCM8: {8x2x5 cell}
> > > >
> > > > >> a2 = load('all_RCMs2')
> > > >
> > > > a2 =
> > > >
> > > > RCM8: {8x2x5 cell}
> > > > RCM11: {8x2x5 cell}
> > > >
> > > > The question is, why does it happen? What does the order depend on? "help save" was not helpful.
> > > >
> > > > (I know how to change the order of fields in a structure, but this is not the point).
> > > >
> > > > Thanks,
> > > >
> > > > Yuri
> > > >
> > > >
> > >
> > >
> > > Try using isequal (documented) instead of isidentical (undocumented). I
> > > believe isequal doesn't care about field order in structs (and
> > > isidentical does). I realized that begs the question about the order
> > > they are written.
> > >
> > > --
> > > Loren
> > > http://blogs.mathworks.com/loren
> >
> > Thanks Loren,
> >
> > OK, let's forget about isequal / isidentical. But the question remains. Here is a statement:
> >
> > save all_RCMs RCM8 RCM11
> >
> > In one case it saves variables RCM8 and RCM11 in such a way that
> >
> > >> fnam = fieldnames(load('all_RCMs.mat'))
> >
> > fnam =
> >
> > 'RCM8'
> > 'RCM11'
> >
> > In another scenario (with identical RCM8 and RCM11) THE SAME LINE generates file all_RCMs.mat such that fnam gets flipped:
> >
> > >> fnam = fieldnames(load('all_RCMs.mat'))
> >
> > fnam =
> >
> > 'RCM11'
> > 'RCM8'
> >
> > Why does it happen, what does the order depend on? I need to have control over this.
> >
> > Thanks,
> >
> > Yuri
> >
> >
>
> Why do you need control over the order?
>
> --
> Loren
> http://blogs.mathworks.com/loren

I have another program, which takes all_RCMs.mat as input, executes this statement

fnam = fieldnames(load('all_RCMs.mat'))

and assumes that fnam(1) is what was output first, and fname(2) is second. This has always worked until I discovered that for a weird reason it became the other way around.

I have other cases with more variables instead of just two. Of course, I can always circumvent this by searching for a required variable name. Yet is very strange that the result of SAVE depends on something random. Or does it?

Subject: strange behaviour of save

From: Walter Roberson

Date: 31 Oct, 2008 15:15:53

Message: 6 of 7

Yuri Geshelin wrote:
 
> I have another program, which takes all_RCMs.mat as input, executes this statement
 
> fnam = fieldnames(load('all_RCMs.mat'))
 
> and assumes that fnam(1) is what was output first, and fname(2) is second.

Well, don't do that then!

When you do that, the load is going to take place and return a structure and the
fieldnames of that structure are going to be returned into fnam, and then the
contents of the structure are going to be discarded. So if you want to use the
contents you would have to do another load(). That is an inefficient design.

If the field order is important for some reason, then save the required fieldname
order as part of a .mat file (as a cell string array) and load it in and use
it in your program.

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: strange behaviour of save

From: Kelly Kearney

Date: 31 Oct, 2008 17:25:04

Message: 7 of 7


> > In one case it saves variables RCM8 and RCM11 in such a way that
> >
> > >> fnam = fieldnames(load('all_RCMs.mat'))
> >
> > fnam =
> >
> > 'RCM8'
> > 'RCM11'
> >
> > In another scenario (with identical RCM8 and RCM11) THE SAME LINE generates file all_RCMs.mat such that fnam gets flipped:
> >
> > >> fnam = fieldnames(load('all_RCMs.mat'))
> >
> > fnam =
> >
> > 'RCM11'
> > 'RCM8'
> >
> > Why does it happen, what does the order depend on? I need to have control over this.
> >
> > Thanks,
> >
> > Yuri
 

>
> Why do you need control over the order?
>
> --
> Loren
> http://blogs.mathworks.com/loren


The one time field order would be a problem is when you need to assign a structure to a single index of a similar multidimensional structure. For example:

C = struct('a', {1 2 3}, 'b', {3 4 5});
A = struct('a', 6, 'b', 7);
B = struct('b', 8, 'a', 9);
C(1) = A; % No problem here...
C(2) = B; % ... but this won't work
??? Subscripted assignment between dissimilar structures.

It's not difficult to circumvent this using explicit concatenation:

C = [C(1) B C(3)];

but I it's a pain when field order gets reversed when you aren't expecting it, as with the save/load example.

Anyway, I just brought up this example because I am also curious how the save command makes it's choice of variable order, and this could provide a reason for needing this info.

-Kelly

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