Variable changes value after using load

조회 수: 2 (최근 30일)
Elías Heimisson
Elías Heimisson 2014년 6월 13일
댓글: dpb 2014년 6월 13일
I've been writing a script that runs a function with numerous subfunctions and I've been careful to not use global variables. At one point in the main function a few matrices saved as .mat are loaded if they do not exists in the workspace. The strange thing is that a variable of mine: maxnumsource = 2; That is one of the input parameters in that defines in the script that runs the main function changes value and is somehow redefined as maxnumsource = 1; after doing the load command. I wrote out the values of the variable right before and after the load and somehow it's redefined! Now the even stranger thing is that when I changed the name to maxnumsources with the added 's' this didn't happen anymore. So I 'fixed' the problem but I would like to know why this happens so I can avoid this in the future. And before anyone asks: No the loaded variables were not named maxnumsource.
Thanks
  댓글 수: 4
Elías Heimisson
Elías Heimisson 2014년 6월 13일
My bad, this was actually a very silly mistake. When I saved the matrix which I then load I forgot to include the variable name. So all variables that were in the workspace when I calculated this matrix were saved including maxnumsource. This matrix was calculated in a seperate script so I was looking for the bug in the wrong place.
dpb
dpb 2014년 6월 13일
Told ya'... :)
Easy mistake to make; no problem...

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2014년 6월 13일
Always accept the results of load() into a structure so that you don't poof variables into existence and cause problems like you did.
storedStructure = load(fullFileName);
var1 = storedStructure.var1;
var2 = storedStructure.var2;
For example if there was a var3 in there, it's not going to blast over any var3 that I already have because I didn't explicitly allow it.
  댓글 수: 1
dpb
dpb 2014년 6월 13일
Most excellent point, IA! +1

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by