Prev: Subcript
Next: CRC-32 for IEEE 802.11-2007
From: Alain on 14 Jan 2010 12:19 I am having a strange problem. here is the scenario: in a function I do the following: load a mat file that loads the variable X call function(X). Matlab comes back saying X not defined. If I do the following; load a mat file that loads the variable X whos X; call function(X). it works ?!?!?!?!?! I know that the details are important, but I am curious to see if anybody has an explanation. I am puzzled ! And the kicker is that I have to do this only once. I a repeat the script, the second time around I do not need the whos.....!?!?!
From: Matt J on 14 Jan 2010 12:34 "Alain " <berdoz(a)code7136.nrl.navy.mil> wrote in message <hinjm8$k5f$1(a)fred.mathworks.com>... > I am having a strange problem. > here is the scenario: > in a function I do the following: > > load a mat file that loads the variable X > call function(X). > > Matlab comes back saying X not defined. > If I do the following; > > load a mat file that loads the variable X > whos X; > call function(X). > > it works ?!?!?!?!?! > > I know that the details are important, but I am curious to see if anybody has an explanation. I am puzzled ! And the kicker is that I have to do this only once. I a repeat the script, the second time around I do not need the whos.....!?!?! See if it makes a difference to load X this way S=load(FileName); X=S.X;
From: Matt J on 14 Jan 2010 13:00 "Alain " <berdoz(a)code7136.nrl.navy.mil> wrote in message <hinjm8$k5f$1(a)fred.mathworks.com>... > I am having a strange problem. > here is the scenario: > in a function I do the following: > > load a mat file that loads the variable X > call function(X). > > Matlab comes back saying X not defined. > If I do the following; > > load a mat file that loads the variable X > whos X; > call function(X). > > it works ?!?!?!?!?! > > I know that the details are important, but I am curious to see if anybody has an explanation. I am puzzled ! And the kicker is that I have to do this only once. I a repeat the script, the second time around I do not need the whos.....!?!?! A few more items to be clarified. 1. Which function in the stack is claiming that X is not defined? Is it function(), the one X is being passed to or is it the one which is issuing the call to function()? Optimally, you would copy/paste the error messages for us. 2. First you said these commands are being executer by a function. Then later, you called it a "script" . The distinction is important, so please clear that up.
From: Alain on 14 Jan 2010 15:30 "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hinm3o$odt$1(a)fred.mathworks.com>... > "Alain " <berdoz(a)code7136.nrl.navy.mil> wrote in message <hinjm8$k5f$1(a)fred.mathworks.com>... > > I am having a strange problem. > > here is the scenario: > > in a function I do the following: > > > > load a mat file that loads the variable X > > call function(X). > > > > Matlab comes back saying X not defined. > > If I do the following; > > > > load a mat file that loads the variable X > > whos X; > > call function(X). > > > > it works ?!?!?!?!?! > > > > I know that the details are important, but I am curious to see if anybody has an explanation. I am puzzled ! And the kicker is that I have to do this only once. I a repeat the script, the second time around I do not need the whos.....!?!?! > > > > A few more items to be clarified. > > 1. Which function in the stack is claiming that X is not defined? Is it function(), the one X is being passed to or is it the one which is issuing the call to function()? > Optimally, you would copy/paste the error messages for us. > > 2. First you said these commands are being executer by a function. Then later, you called it a "script" . The distinction is important, so please clear that up. fair enough. 1. the error comes in the function that is issuing the call and the error message says: " Undefinied function or variable 'X' " 2. sorry, I am using script loosely and it's a mistake. I should have said function But it seems to work properly when I use the proposed solution: S = load... then funtion(S.X) I am sure I do not know the difference, but can not argue with success....
From: Matt J on 14 Jan 2010 16:06
"Alain " <berdoz(a)code7136.nrl.navy.mil> wrote in message <hinusf$hj1$1(a)fred.mathworks.com>... > But it seems to work properly when I use the proposed solution: > S = load... > then funtion(S.X) > I am sure I do not know the difference, but can not argue with success.... I can't know precisely what's going on without seeing your full code, and also possibly without seeing what function names are on your path. Generally speaking though, it is a hazardous coding practice to introduce variables into a workspace without an explicit assignment statement, as for example using load(), eval(), evalin(), assignin(), etc... This practice, called "poofing" in some circles, has some known dangers discussed for example in the threads below. None of them seem to match up precisely with what you're reporting, but again, I would need to know a lot more about what's in your workspace... http://www.mathworks.com/matlabcentral/newsreader/view_thread/244639#628695 http://www.mathworks.com/matlabcentral/newsreader/view_thread/270127 In any case, it was clear to me that whos() in some way forces MATLAB to rescan the workspace for any poofed variables it didn't initially catch, which is why it apparently corrected the problem for you. The bottom line, for you, is that you should always pipe the output of load to a structure and then if need be unpack the fields of the structure into separate variables S=load(...) A=S.A; B=S.B; C=S.C, etc... as I had proposed. If you find this cumbersome, I created this FEX tool to cut down on the work: http://www.mathworks.com/matlabcentral/fileexchange/26216-structure-fields-to-variables |