Can you create GUI class elements in a loop?
8 次查看(过去 30 天)
显示 更早的评论
Hi everybody. I am writing a GUI in which 32 toggle buttons function as status indicators. I have chosen to use toggle buttons because of their clear appearance. However, creating 32 buttons in essentially the same manner is very tedious and cluttered. Is there more efficient a way I could do this?
Here is all the relevant information about my program. I am defining everything in a handle class:
classdef x_GUI < handle
properties
bit1
bit2
....
These properties are then initialized in a function:
function app = x_GUI
Now creating the elements in this function the tedious way works fine. For example,
app.bit1 = uicontrol('Style','togglebutton',....)
This correctly displays the elements in the GUI, allows me to reference them later (i.e. get(app.bit1,'Value')) and outputs "x_GUI with properties bit1: [1x1 UIControl] ..."
when the script is executed to let me know that the property in the class has been altered. But I have to do this 32 times.
So when I tried to write a loop to save time and code, the element displayed properly, but in "x_GUI with properties bit1: [ ]..."
meaning that the property in the class was not edited. I tried using a for loop with
sprintf(app.bit%d,i) = uicontrol(...)
and creating an array
bit_handles = {app.bit1 ...} and then doing bit_handles{i} = uicontrol(...).
Both created the elements properly, but neither actually referenced the property in the class meaning bit1: [ ] still. Essentially, my problem is actually creating handles for the elements that are based off of the properties in the class.
Does anyone know a way I could effectively reference class properties in a for loop? Thank you very much for your time!
0 个评论
回答(1 个)
Jan
2015-6-18
I'm not sure, what your problem is. So just some thoughts:
"sprintf(app.bit%d,i) = uicontrol(...)"? Do you mean:
for k = 1:32
app.(sprintf('bit%d', k)) = uicontrol(...)
end
Note that you hide an index in the name of the fields. Why not using an array instead?
for k = 1:32
app.bit(k) = uicontrol(...)
end
Then you can obtain and set the properties of all objects directly:
get(app.bit, 'Value')
set(app.bit, 'BackgroundColor', [1,1,0])
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!