Filter löschen
Filter löschen

Error using ==> eval

13 Ansichten (letzte 30 Tage)
M K
M K am 22 Sep. 2016
Bearbeitet: Stephen23 am 19 Jun. 2019
Hi,
I am writing a code to compare several nested structures. Basically I need to compare all the values in cell (x,1) in every structure to the cell (x,1) in every other structure.
My issue right now is with organizing the output. I need the output to be a structure (output) that contains a structure for each subject (subject1) and I want that structure to be named according to a looping variable, x (representing the subject #) and then within that subject1 structure, I want structures for each of the comparison subjects (subject2), also named according to a looping variable, y.
Here is what I have written so far:
function output=hcp_compare(hcp)
output=zeros(843,1);
%for all subjects in x
for x=1:length(hcp)
%create subject name
subject1=eval(['subj' num2str(x)]);
%insert main subject name
output(x,1).(subject1)(1,1).name=hcp(x,1).name;
%then loop through evry subject and compare to x
for y=1:length(hcp)
%loop through evry cell in connMatVect and compare for evry xypair
subject2=eval(['subj' num2str(y)]);
for z=1:35778
output(x,1).(subject1)(2,1).comparisons(x,y).(subject2)(z,1)=(hcp(x,1).connMatVect(z,1)-hcp(y,1).connMatVect(z,1));
end
end
end
end
I want (subject1) and (subject2) to be replaced by subjx and subjy. But when I run the code, I get the following error:
??? Error using ==> eval
Undefined function or variable 'subj1'.
Error in ==> hcp_compare at 10
subject1=eval(['subj' num2str(x)]);
So I believe I'm missing something or not understanding something. Can anyone give some pointers?
  1 Kommentar
Stephen23
Stephen23 am 22 Sep. 2016
"I believe I'm missing something or not understanding something. Can anyone give some pointers?"
Don't use eval.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 22 Sep. 2016
Bearbeitet: Stephen23 am 19 Jun. 2019

Weitere Antworten (1)

dpb
dpb am 22 Sep. 2016
DON'T DO THIS!!! See <generate-field-names-from-variables> for how to do this instead.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by