Call on Dynamic Variable

조회 수: 2 (최근 30일)
Janae Csavina
Janae Csavina 2015년 3월 16일
답변: Adam 2015년 3월 16일
I have data ingest for sensors 1 through n which I identify as sensor1 through sensorn. I am wanting to write a for loop to control which data array I am calling.
Here is an example of what I am trying to accomplish:
for g=1:19
nanRows=any(isnan(sensor(g)),2);
sensor(g)(nanRows, :) = [];
Esensor(g)=average-sensor(g);
SEsensor(g)=Esensor(g).^2;
ssensor(g)=sensor(g);
g=g+1;
end
I am realizing that (g) does not provide a variable accordingly. Any suggestions?
Thanks!
Janae

답변 (2개)

James Tursa
James Tursa 2015년 3월 16일
Please see this post about having variables named sensor1, sensor2, sensor3, etc and then trying to process them in a loop:
Bottom line is that it can be done using the eval function, but it is very messy as you are finding out. It would be much better to restructure your data into cell or struct arrays. They will be much easier to process in loops, and there will be a variety of supplied functions in MATLAB to deal with data stored this way.

Adam
Adam 2015년 3월 16일
Similar question, similar answer:
If you can't use a normal double array then use a cell array or a struct with dynamic fields as a 2ndd choice.

카테고리

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