HELP eval - Undefined function 'eval' for input arguments of type 'cell'. - find structure (from workspace) with part of name and accessing it
5 views (last 30 days)
Show older comments
Hello, I am currently writing a code to analyse blood pressure. The blood pressure has been extracted using another language, the code renamed my channels a bit weirdly. I just need to modify my matlab code to make it flexible to all those weird names .. and I am a bit stuck.
My matlab code is looped on my participants.
For each participant, I load a .mat file (easy).
As I said the channel are weirdly renamed :
name of channel1: XYPlot *1_01_1*__Ch1
name of channel2: XYPlot *1_01_1*__Ch2
name of channel3: XYPlot *1_01_1*__Ch3
The bold part is always different between subjects.
**MY GOAL
To define:
time_Spike= channel2.xvalues;
SBP_peaks_values = channel2.yvalues;
**MY SCRIPT
namesWorkspace = who;
outStr = regexpi(namesWorkspace, 'Ch2');
ind = ~cellfun('isempty',outStr);
var = namesWorkspace(ind);
str={var};
out=str{1};
channel2=eval(out);
time_Spike= channel2.xvalues;
SBP_peaks_values = channel2.yvalues;
**OUTCOME
But I have a problem with eval
"Undefined function 'eval' for input arguments of type 'cell'."
I am sure there is another easier way to do what I want to do,
Your help and feedback would be very appreciated :)
thanks
Sophie
1 Comment
Image Analyst
on 22 Jul 2017
Edited: Image Analyst
on 22 Jul 2017
Sophie, you forgot to attach the .mat file so I can't do anything (yet).
"out" appears to be the contents of a cell. We don't know what data type it is - it could be anything. But there is no reason to send in into eval(). Why did you do that???
Accepted Answer
Jan
on 22 Jul 2017
Edited: Jan
on 22 Jul 2017
The topic of the dynamic creation of variables is discussed very frequently. The answer is always the same: Don't do this. If causes more problems than it solves. See this exhaustive explanation: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval .
The actual problem here seems to be:
var = namesWorkspace(ind);
str={var};
out=str{1};
channel2 = eval(out);
It would work with:
variable = namesWorkspace{ind}; % "var" is the varianze command
channel2 = eval(variable);
But this is still a shot in your knee. Much better catch the output of load:
Data = load(FileName);
Fields = fieldnames(Data);
ch2Str = regexpi(namesWorkspace, 'Ch2');
ind = ~cellfun('isempty',outStr);
channel2 = Data.(Fields{ind});
Now your workspace is not polluted by variables from the MAT file and no eval confuses the JIT acceleration. You know while reading the source code where the variables are coming from and this supports an efficient debugging.
In short: Don't EVAL!
More Answers (1)
Steven Lord
on 22 Jul 2017
DON'T use eval. load your data into a struct array and use fieldnames and dynamic field names to access the data in the struct array.
See Also
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!