Using set for multiple objects in a loop

4 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2017-2-20
How would the sensors be constructed? For example have you constructed them as arduino i2c devices?

请先登录,再进行评论。

采纳的回答

bmeyer
bmeyer 2017-2-20
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...

更多回答(1 个)

Walter Roberson
Walter Roberson 2017-2-20
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.

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by