Dynamically assign structure names and loop them in fuction

조회 수: 4 (최근 30일)
George
George 2015년 3월 11일
편집: Stephen23 2023년 9월 12일
Hello
I am trying to dynamically assign some of my variable names which are structures and them pass them through a script although I get an error
DATA=who('DATA*')
ORIG=who('ORIGINAL*')
countS=length(DATA)
countC=length(ORIG)
if countS==countC;
for i=1:countS
figure
scatter(DATA{i,1}.H,ORIGINAL{i,1}.H)
lm(i)=polyval(DATA{i,1}.H,ORIGINAL{i,1}.H)
end
end
Though i get Attempt to reference field of non-structure array. The structures themselves contain fields that i want to iterate through other processes, am I doing something wrong?
thanks
  댓글 수: 1
Stephen23
Stephen23 2015년 3월 11일
편집: Stephen23 2015년 3월 11일
Do not use dynamically names variables in MATLAB. Put your variables in a cell array instead.
The answers will explain why, and please understand them if you want to write good code.
This is the third time that you have asked a question that used dynamically named variables and faced a difficult problem because of that choice: each time the advice given was "do not use dynamically named variables", and reasons were given.

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

채택된 답변

Stephen23
Stephen23 2015년 3월 11일
편집: Stephen23 2023년 9월 12일
  댓글 수: 2
George
George 2015년 3월 11일
I am using structures, the names are created by the structures loaded in a previous load sequence/generation. The structures themselves have inside them various data.I only use the dynamic naming so as to automate the loading.clearing processing process
Stephen23
Stephen23 2015년 3월 12일
편집: Stephen23 2015년 3월 15일
Every time you use dynamically named variables you write a question here: "why is this difficult?", and the advice is "don't do this". Three times with the same problem and three times the same answer.

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

추가 답변 (1개)

James Tursa
James Tursa 2015년 3월 11일
편집: James Tursa 2015년 3월 11일
The result of the who command simply gives you variable names as a cell array of strings ... it doesn't give you the variables themselves. Thus, DATA{i,1} is a string that is the name of a variable ... it is not the variable itself so you can't use .H syntax to get at the H field. You will probably need to use something like eval to get at the variable. E.g.,
scatter(eval([DATA{i,1} '.H']),eval([ORIGINAL{i,1} '.H']))
lm(i) = polyval(eval(DATA{i,1} '.H']),eval([ORIGINAL{i,1} '.H']));
This is admittedly ugly, but that is typically what you end up with when you have a multitude of variables in your workspace with similar names that you need to loop over. A better practice is often to have such variables gathered up into a single cell array instead. This makes accessing them in a loop much easier, and allows the use of MATLAB functions that operate on cell arrays (often avoiding loops altogether). E.g.,

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by