HELP eval - Undefined function 'eval' for input arguments of type 'cell'. - find structure (from workspace) with part of name and accessing it
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2017-7-22
编辑:Image Analyst
2017-7-22
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???
采纳的回答
Jan
2017-7-22
编辑:Jan
2017-7-22
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!
更多回答(1 个)
Steven Lord
2017-7-22
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!