Why two variables loading the same .mat file are not equal ?

2 ビュー (過去 30 日間)
Florian Bidaud
Florian Bidaud 2022 年 2 月 17 日
コメント済み: Rik 2022 年 2 月 17 日
Hi,
I found something weird today and couldn't find an explanation on the forum.
When I load the same matlab file twice, and try to check if they are equal, the output is False.
In my situation, a and b are structures with large arrays inside, and I manually checked that they are actually equal.
Where does this come from ? Is there another way to quickly check the equality ?
Thanks !
a = load('myFile.mat')
b = load('myFile.mat')
isequal(a,b)
ans =
logical
0
  3 件のコメント
Florian Bidaud
Florian Bidaud 2022 年 2 月 17 日
編集済み: Florian Bidaud 2022 年 2 月 17 日
Sorry I can't provide the full file because this is confidential data... I know it won't help..
I can show you the structure of the file
isequal(a.simu_param,b.simu_param) and isequal(a.FEA_data,b.FEA_data) give 1
But the others give 0.
Inside the 3 other structures are stored double arrays, which are actually equal when I check manually.
Do you think it could come from double equality test ? I know sometimes things like 1.500 == 1.5 give False. But in this situation, they are supposed to be the exact same ones.
Image Analyst
Image Analyst 2022 年 2 月 17 日
Can you at least show us a screenshot of you doing those 3 lines of code? And have both the code in the editor window, and the command window below in the picture. We need to make sure that they are both exactly the same filename, and not different somehow (like one ends in a space or something).

サインインしてコメントする。

採用された回答

Rik
Rik 2022 年 2 月 17 日
編集済み: Rik 2022 年 2 月 17 日
I guess boldly:
There might be a NaN value hidden somewhere in your data:
isequal(NaN,NaN)
ans = logical
0
You might be interested in my ComputeNonCryptHash function.
s1=struct('x',NaN);s2=s1;
isequal(s1,s2)
ans = logical
0
isequal(ComputeNonCryptHash(s1),ComputeNonCryptHash(s2))
ans = logical
1
  2 件のコメント
Florian Bidaud
Florian Bidaud 2022 年 2 月 17 日
Oh I didn't no about NaN values making isequal False, I do indeed have NaN values in my arrays, so it must be that !
Thank you !
Walter Roberson
Walter Roberson 2022 年 2 月 17 日

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2022 年 2 月 17 日
You can get results that are not isequal() when you load data that does not serialize exactly.
Consider for example if you save a graphics object, then when you load() it is going to create two different versions of the graphics object.

Florian Bidaud
Florian Bidaud 2022 年 2 月 17 日
I finally used isequaln to get the result I wanted, thank you all.
  3 件のコメント
Florian Bidaud
Florian Bidaud 2022 年 2 月 17 日
As Rik said, it was because I had NaN values in some of my arrays, and isequal(NaN,NaN) = 0.
Rik
Rik 2022 年 2 月 17 日
And Walter added another explanation: if the mat file contains a graphics object the second load might create a new object, instead of remembering that it has already that object in memory.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by