Using set for multiple objects in a loop

6 visualizaciones (últimos 30 días)
bmeyer
bmeyer el 20 de Feb. de 2017
Respondida: bmeyer el 20 de Feb. de 2017
Hi,
I am using guide and trying to update the string for 4 edit fields on my GUI. Here is some pseudo-code (not functional) that hopefully conveys my question:
handles.nSensors = 4;
for i = 1:handles.nSensors
readVal(handles.sensorValue(i)); % read value of sensor 'i'
set(handles.sensorLabel{i},'String',num2str(handles.sensorValue(i))); % update GUI text field 'i' with value of sensor 'i'
end
My issue is with the second line in the for loop; how can I rename the first argument each iteration? I'm not sure I understand what type of object the first argument is. Is it a function handle?
Thanks for the help, if this question is unclear please let me know and I will try to explain better.
  1 comentario
Walter Roberson
Walter Roberson el 20 de Feb. de 2017
How would the sensors be constructed? For example have you constructed them as arduino i2c devices?

Iniciar sesión para comentar.

Respuesta aceptada

bmeyer
bmeyer el 20 de Feb. de 2017
Thanks, I figured it out. I had been trying to iterate through a cell array of function handles, which was correct, but the issue was that I was not feeding it null arguments.
For example, I was trying this:
set(handles.sensorLabel{i},'String',num2str(handles.sensorValue(i)));
When I should have done this:
set(handles.sensorLabel{i}(),'String',num2str(handles.sensorValue(i)));
Stupid parentheses...

Más respuestas (1)

Walter Roberson
Walter Roberson el 20 de Feb. de 2017
There are some kinds of objects that you can construct vectors out of; for example you can have a vector of graphics objects.
However, some kinds of objects have attached meaning to () syntax that is something different than selection between multiple similar objects; you cannot create arrays of those objects. For example, function handles use () to indicate that function is to be invoked, so you cannot create a normal array of function handles.
If you happen to be using an object that you cannot create arrays of, then you will need to place the objects into cell arrays. You can, for example, have cell arrays of function handles.

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