MATLAB Answers

Sharon
0

How to load a .mat file and make all the variables from that .mat file to be global variables?

Asked by Sharon
on 13 Nov 2018
Latest activity Commented on by Sharon
on 14 Nov 2018
I loaded the .mat file and use the variables in the rest of the codes. Even though the file is loaded outside the for loop, it shows the variable is not defined during the calculation, moreover, the very same variable is used in even earlier calculation. I think the problem is caused by the parfor, so I would like to set the loaded variables as global to see if the problem can be solved.
The part of the code is
load('SobolParameter3Layer.mat');%input: numLayer numPar N Par Para Parac AllCol numCol totalCol
for j=1:numPar
comb=AllCol{j};
SF=zeros(N,size(AllCol{j},1));WF=zeros(N,size(AllCol{j},1));
parfor i=1:size(comb,1)
para=Par(:,1+numPar:2*numPar);
para(:,comb(i,:))=Par(:,comb(i,:));
[SF(:,i),~,WF(:,i)]=BandStruS(para,numLayer);
end
Start(:,numCol(1,j):numCol(2,j))=SF;Width(:,numCol(1,j):numCol(2,j))=WF;
end
the code can run without problem for the first for loop, but when it comes to parfor, the "numPar" becomes undefined.

  1 Comment

"How to load a .mat file and make all the variables from that .mat file to be global variables?"
"...I would like to set the loaded variables as global to see if the problem can be solved."
Global variables cause more problems than they solve. They make code slow, buggy, and very hard to debug. Combining this with dynamically accesssed variable names would be a double-mix of bad code design:

Sign in to comment.

Products


Release

R2015a

2 Answers

Answer by Stephen Cobeldick on 14 Nov 2018
Edited by Stephen Cobeldick on 14 Nov 2018
 Accepted Answer

Always load any .mat file into an output argument (which is a structure):
S = load(...);
This avoids several issues related to overwriting variables, undefined variables, and calling an unexpected function/class/variable. You should be doing this every time you load a .mat file.
Then within your code you simply access the fields of that structure by using dot notation:
S.numLayer
S.numPar
... etc

  1 Comment

Sign in to comment.


Answer by Adam Danz
on 13 Nov 2018

You don't want to make all the mat variable global. That will open a can of worms. If variables in the mat file are needed in other functions, pass those variables to the function.
Also, name the variables you're loading.
load('SobolParameter3Layer.mat', 'numLayer', 'numPar', 'N', 'Par', 'Para', 'Parac', 'AllCol', 'numCol', 'totalCol');
If you try to load a variable that isn't in the mat file you'll get a warning.
load('SobolParameter3Layer.mat', 'fubar')
Warning: Variable 'fubar' not found.
If you make these changes and still get an error, please comment including the full copy-pasted error message and what time is producing it.

  3 Comments

Thank you very much for your answer. But the error this time appear to be as followed
Error using SobolS (line 63)
An UndefinedFunction error was thrown on the workers for 'numPar'. This might
be because the file containing 'numPar' is not accessible on the workers. Use
addAttachedFiles(pool, files) to specify the required files to be attached.
See the documentation for 'parallel.Pool/addAttachedFiles' for more details.
Caused by:
Undefined function or variable 'numPar'.
where SobolS is the function I defined. And I had made sure to put "SobolParameter3Layer.mat" in the same directory as the main program and all the functions.
When you loaded your variables, are you sure 'numPar' was in the mat file and loaded properly?
Also, why is the following line within the loops when it doesn't rely on the value of i or j? Can you move that line so it's prior to the loops?
para=Par(:,1+numPar:2*numPar);
Hi Adam,
Yes. I am sure the .mat file has the "numPar" and other variables, because I checked it by saving the loaded file in another .mat file and those variables are there.
I put the line outside the for and parfor loop, and the problem comes to the next variable "numLayer".
Error using SobolS (line 66)
An UndefinedFunction error was thrown on the workers for 'numLayer'. This
might be because the file containing 'numLayer' is not accessible on the
workers. Use addAttachedFiles(pool, files) to specify the required files to be
attached. See the documentation for 'parallel.Pool/addAttachedFiles' for more
details.
Caused by:
Undefined function or variable 'numLayer'.
I think as long as I use the loaded variable within parfor, I will have some trouble. I think I still need to try to make those loaded variables global, so it won't have problem during parfor.
Thank you very much for your help.

Sign in to comment.