Call on Dynamic Variable

2 visualizaciones (últimos 30 días)
Janae Csavina
Janae Csavina el 16 de Mzo. de 2015
Respondida: Adam el 16 de Mzo. de 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

Respuestas (2)

James Tursa
James Tursa el 16 de Mzo. de 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 el 16 de Mzo. de 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.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by