Multiple Errors with GUI Layout Toolbox and arrays of classes

4 visualizaciones (últimos 30 días)
Nicholas
Nicholas el 19 de Nov. de 2012
I have already posted this here but want some more people to be able to see it to better my chances of getting it fixed.
I'm having a few issues with UIExtras which may be a result of my complete lack of MATLAB expertise. I have been trying to set up a GUI that has a window with multiple tabs each with multiple HBoxes that are nearly identical. The total number of objects will reach 200 by the time I'm done. Due to this, I wanted to create a couple arrays of these objects so that I can index them and create them in a manner that's much easier to write and read. However, I've run into several issues.
First up, this code runs as a script perfectly fine, but wrap it in a function, and it fails:
sf = figure();
tp = uiextras.TabPanel();
vb1 = uiextras.VBox('Parent', tp);
vb2 = uiextras.VBox('Parent', tp);
vb3 = uiextras.VBox('Parent', tp);
but1 = uicontrol('Style', 'pushbutton', 'Parent', vb2);
but2 = uicontrol('Style', 'pushbutton', 'Parent', vb2);
%hb = zeros(1,3); buttons = zeros(2,3); %%this is the array init line that 'fixes' the issue, see below
for i = 1:3
hb(i) = uiextras.HBox('Parent', vb3);
buttons(1,i) = uicontrol('Style', 'pushbutton', 'Parent', hb(i));
buttons(2,i) = uicontrol('Style', 'pushbutton', 'Parent', hb(i));
set(hb(i), 'Spacing', 0, 'Padding', 0, 'Sizes', [-1 -2], 'MinimumSizes', [20 40]);
end
%get(hb(1))
This does exactly what I want it to with no immediately apparent errors while
function uiextrastestA() %%saved as correct file name
sf = figure();
tp = uiextras.TabPanel();
vb1 = uiextras.VBox('Parent', tp);
vb2 = uiextras.VBox('Parent', tp);
vb3 = uiextras.VBox('Parent', tp);
but1 = uicontrol('Style', 'pushbutton', 'Parent', vb2);
but2 = uicontrol('Style', 'pushbutton', 'Parent', vb2);
%hb = zeros(1,3); buttons = zeros(2,3); %%this is the array init line that 'fixes' the issue, see below
for i = 1:3
hb(i) = uiextras.HBox('Parent', vb3);
buttons(1,i) = uicontrol('Style', 'pushbutton', 'Parent', hb(i));
buttons(2,i) = uicontrol('Style', 'pushbutton', 'Parent', hb(i));
set(hb(i), 'Spacing', 0, 'Padding', 0, 'Sizes', [-1 -2], 'MinimumSizes', [20 40]);
end
%get(hb(1))
end
returns
>Error using uicontrol
>Conversion to double from unknown is not possible.
>
>Error in uiextrastestA (line 14)
>buttons(1,i) = uicontrol('Style', 'pushbutton', 'Parent', hb(i));
When I eliminate the for loop and manually repeat it 3 times, I get the same results (script works, function fails). I would love for someone to explain why this is an issue and help me fix it more elegantly. As you can see in the code, I discovered array initialization. Once I temporarily patched that issue, I promptly discovered another.
Run the following code as EITHER a script OR a function (this one has array initialization):
sf = figure();
tp = uiextras.TabPanel();
vb1 = uiextras.VBox('Parent', tp);
vb2 = uiextras.VBox('Parent', tp);
vb3 = uiextras.VBox('Parent', tp);
but1 = uicontrol('Style', 'pushbutton', 'Parent', vb2);
but2 = uicontrol('Style', 'pushbutton', 'Parent', vb2);
hb = zeros(1,3); buttons = zeros(2,3);
for i = 1:3
hb(i) = uiextras.HBox('Parent', vb3);
buttons(1,i) = uicontrol('Style', 'pushbutton', 'Parent', hb(i));
buttons(2,i) = uicontrol('Style', 'pushbutton', 'Parent', hb(i));
set(hb(i), 'Spacing', 0, 'Padding', 0, 'Sizes', [-1 -2], 'MinimumSizes', [20 40]);
end
%get(hb(1))
returns the error:
>Error using hg.uipanel/set
>The name 'Spacing' is not an accessible property for an instance of class 'uipanel'.
>
>Error in uiextrastestB (line 15)
>set(hb(i), 'Spacing', 0, 'Padding', 0, 'Sizes', [-1 -2], 'MinimumSizes', [20 40]);
Class UIPanel? That's an HBox, not a UIPanel... right? Alas, when I uncommented the last line (get(hb(1))) I get:
(with array initialization)
>Type = uipanel
(without array initialization)
>Type = uiextras.HBox
Aaaand I'm lost.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If anyone can help me here, it'd be greatly appreciated.

Respuestas (1)

Walter Roberson
Walter Roberson el 19 de Nov. de 2012
It looks to me as if uiextras.HBox() returns objects that are not handle graphics handles. Handle graphics handles are dataclass double and can be stored in numeric arrays.
It looks as if when you initialize hb to zeros(), which is a numeric array, and then try to store uiextras.HBox() in them, that the uiextras.HBox code converts the reference from object to handle graphics handle of a uipanel. Which cannot then have its Spacing set as it does not have a Spacing property. If you initialize then you need to initialize to the datatype that is going to be assigned into the array.
The error you get when you do not initialize, "Conversion to double from unknown is not possible", looks to me as if the problem is in the Parent property of the uipanel, that it is expecting a handle graphics handle (type double) and is failing because it is getting an object instead. Mind you, this idea is not fully compatible with the idea that there is automatic conversion going on in the other case.
For now, test without the array initialization, and stop after the first element has been assigned, and ask for class(hb) to see what is being assigned.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by