Error due to using the same varname in mat file and matlab function

I have a mat file (level1.mat) having the following structure. a = 187817x28 tar = 14931x5 zes = 1167x4
To read this file, I used the following procedure. load('level1.mat') a0=a; tar0 = tar; zes0=zes; clear a tar zes
But it gave me an error message like Error using tar (line XX) Not enough input arguments. Error in reader_level1 (line XX) tar0 = tar;
I think the problem might be caused by the duplicate variable name (tar) in mat file and function (tar: to compress file). However, I cannot solve this problem. I do not want to change original mat file but change my code. Could you please help me how to fix this problem? Thank you very much in advance.

 採用された回答

OCDER
OCDER 2017 年 10 月 11 日

1 投票

Try to load the variables into a structure to prevent "poofing" them into your workspace, which can cause issues with overriding existing variables and functions. See workaround below:
T = load('level1.mat');
a0 = T.a;
tar0 = T.tar;
zes0 = T.zes;
clear T
Also, it seems you're about to label variables dynamically like a0, a1, a2, a3, tar0, tar1, tar2 etc. This will cause issues later when you want to access all a0-N, tar0-N, etc. Instead, use T directly, OR, store variables in a matrix, cell, or structure. Lastly, using "clear" is slow and hard to keep track of which variables to clear/keep, so try to avoid using this if you can.
Read this discussion before making your code:

2 件のコメント

jmok
jmok 2017 年 10 月 11 日
I really appreciate for your clear answer. It works!
OCDER
OCDER 2017 年 10 月 11 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

質問済み:

2017 年 10 月 11 日

コメント済み:

2017 年 10 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by