Undefined function or variable when using load and trying to operate with variables

3 ビュー (過去 30 日間)
Hi: I have posted before asking one question and I didn't really explain myself correctly so not ALL my question was solved: I have a function which saves some variables each time with a different name in the form of a matrix (30x30). This variables are called Z_1,Z_2,Z_3,etc...
save(['Z_' num2str(v)],'z')
Now I want to add these variables which I have created (Z_1,Z_2,etc) so that it gives me a matrix (30x30) called TotalZ for example. This would be done easily enough using:
load('C\wherever.the.variable.is.stored\Z_1');
load('C\wherever.the.variable.is.stored\Z_2');
load('C\wherever.the.variable.is.stored\Z_3');
TotalZ=Z_1+Z_2+Z_3;
But every time I try running this I get the error;
??? Undefined function or variable 'Z_1'.
Error in ==> mapa at 31
TotZ=Z_1+Z_2+Z_3
I am getting a bit frustated as I have clearly defined my variables and loaded them properly (or so I think) so I see no reason for this error. The idea would then be to plot the TotalZ against two variables with a pcolor plot.
Thanks for any help. Guillermo
  1 件のコメント
Guillermo Lopez
Guillermo Lopez 2012 年 4 月 26 日
And it might be a problem as every time it is loaded into the workspace all variables Z_1,Z_2,Z_3 are named the same (z) due to the name of the variable being saved so I suppose every time I load a new one the previous one is deleted...

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

採用された回答

Thomas
Thomas 2012 年 4 月 26 日
Since you are saving all the variables a 'z' try the following..
Z_1=load('C\wherever.the.variable.is.stored\Z_1');
Z_2=load('C\wherever.the.variable.is.stored\Z_2');
Z_3=load('C\wherever.the.variable.is.stored\Z_3');
total=Z_1.z+Z_2.z+Z_3.z
or
load('C\wherever.the.variable.is.stored\Z_1');
Z_1=z;
load('C\wherever.the.variable.is.stored\Z_2');
Z_2=z;
.
.
total=Z_1+Z_2...
  4 件のコメント
Thomas
Thomas 2012 年 4 月 26 日
As Jan explained,, every time you load a new file the variable z is put in Z_1 and Z_2 and so on and not lost..
Did you try option 1?
Guillermo Lopez
Guillermo Lopez 2012 年 4 月 26 日
Ok I get it now. I will try this...

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

その他の回答 (1 件)

Honglei Chen
Honglei Chen 2012 年 4 月 26 日
The first variable in save is the file name, not the variable name. So in each of your files, the variable name is still z. Therefore, after you load all the files, there is still only one z. To do what you want, you need to save those files using different variable name z1,|z2|,... or you load them to a variable like this
load('C\wherever.the.variable.is.stored\Z_1');
Z_1 = z;
load('C\wherever.the.variable.is.stored\Z_2');
Z_2 = z;
load('C\wherever.the.variable.is.stored\Z_3');
Z_3 = z;
TotalZ=Z_1+Z_2+Z_3;
  4 件のコメント
Honglei Chen
Honglei Chen 2012 年 4 月 26 日
eval(['Z_' num2str(v) '=z']);
save(['Z_' num2str(v)],['Z_' num2str(v)])
Walter Roberson
Walter Roberson 2012 年 4 月 26 日
Using eval() is not recommended!!

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

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by