Using set for multiple objects in a loop

조회 수: 6 (최근 30일)
bmeyer
bmeyer 2017년 2월 20일
답변: bmeyer 2017년 2월 20일
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에서 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