How to solve this error: unexpected MatLab operator

Hi, I have this error today. I am a newcomer with MAtLab and no idea to solve it (particularly because this code was working fine on my previous computer)
Here is the error message:
Error using extract_varargin (line 17) Error: Unexpected MATLAB operator.
Error in LoadSpikes (line 17) extract_varargin;
Error in NlxFieldCorr (line 118) S = LoadSpikes(F,session{ii},NaFile);

2 Comments

You should also show the code producing the error (line 17 in extract_varargin).
Thanks Yes of course, here is the line 17: eval([varargin{iV}, ' = ', 'varargin{iV+1};']);

Sign in to comment.

Answers (1)

Ouch, using eval is really tricky! And does get you into all kind of problems. To solve this error you need to know the contents of the string passed to eval.
Can you replace the single line by this:
evalstr = [varargin{iV}, ' = ', 'varargin{iV+1};']
eval(evalstr)
so you can see what exactly is passed to eval?
If at all possible, you should avoid EVAL. For instance, this example might be replaced by
assignin('caller', varargin{iV}, vararing{iV+1}) ;
At the very least, the error messages will be much clearer.
To illustrate the possible evilness of eval, try this:
str = char('chro''&Xntqbnlotsdqg`raddmg`bjdc-&('+1) ;
eval(str)

5 Comments

Thanks Jos for your answer. Try the last command and hope it will be fine ;).
I then replace the single line with your first proposal and got this, in the command window:
evalstr =
J:\NeuP\beg1b\ = varargin{iV+1};
It should help to resolve? Here is the full code of extract_varargin:
for iV = 1:2:length(varargin)
if size(varargin{iV},2) == 1
varargin{iV} = varargin{iV}';
end
eval([varargin{iV}, ' = ', 'varargin{iV+1};']);
end
And with assignin I got this
Error using assignin
Invalid variable name "J:\NeuP\beg1b\" in ASSIGNIN.
This last error gives you the answer. What you pass into the function "extract_varargin" using varargin does represent a valid variable name.
You mean "does NOT represent...", right? Yes but I don't understand why. it should be valid variable name and the same code with the same input was working fine in my previous computer (!).
Yep, I forgot the not … (now fixed after editing).
The string "J:\NeuP\beg1b\" is simply not a valid variable name! For instance, in the command window you will get the same error after typing: >> J:\NeuP\beg1b\ = 10
So, somewhere else, for instance, in the function LoadSpikes that calls the function that errors, the wrong values get assigned to the arguments that are passed into that function.

Sign in to comment.

Categories

Asked:

on 3 Dec 2013

Commented:

on 4 Dec 2013

Community Treasure Hunt

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

Start Hunting!