Call on Dynamic Variable

2 views (last 30 days)
Janae Csavina
Janae Csavina on 16 Mar 2015
Answered: Adam on 16 Mar 2015
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

Answers (2)

James Tursa
James Tursa on 16 Mar 2015
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 on 16 Mar 2015
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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!