필터 지우기
필터 지우기

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
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
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
Sophie
Sophie 2017년 7월 23일
Hi, thanks for your explanation, your trick with eval is working well. thanks.
I tried your last suggestion but "Index exceeds matrix dimensions."

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

추가 답변 (1개)

Steven Lord
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 CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by