Programmatically add AppDesigner uicontrol

I have a CSV file with a list of m-files, and I want an AppDesigner GUI to have 1 button per m-file. Let's pretend all each button does is call "edit(mfile#)" for the appropriate number. Since all controls are explicit properties of the app, it gets very angry trying to create controls inside the startupFcn. It also does not seem to like using the GraphicsPlaceholder to pack multiple controls into a single app property.
I'm using a workaround that is effective for my current need, but might not be next time. I know the maximum number of m-files, and declaring an app property as a button doesn't actually create the button. So, I manually create the maximum properties, then fill as I read the CSV file.
Any other thoughts or suggestions?

回答(2 个)

How's this?
fig = uifigure;
panel = uipanel(fig,'Position',[1 1 500 500]);
bg = uibuttongroup(panel,'Position', [50 50 100 500]);
n = 10;
g = gobjects(n,1);
for ii = 1:n
uibutton('Parent',bg,'Position',[10 ii*30 50 30])
end

5 个评论

I'm sorry, my description was incomplete, and therefore unclear. I would like to keep the GUI in AppDesigner. The rest of the application is greatly aided by the auto-magic in AppDesigner, but I need to add a few buttons on-the-fly.
Thanks for the idea, I appreciate the help.
That's what this does!
Replace fig = uifigure with app.Figure and put the whole thing in a callback.
My apologies again. The fig = uifigure line took my mind outside of AppDesigner. With this suggestion, I'll still have the problem that I can't store the button's handle as an app property.
app.newbutton1 = uibutton(...); so that I can later utilize app.newbutton1
Thanks again.
You can add a property in the app that would be a structure that contains the buttons, you can then change that structure as you add buttons.
I have the same issue . I have created the structure and saved the handle inside the structure:
app.index=app.index+1;
c=uibutton(app.InterfaceTab);
app.a.b(app.index)=c;
But now there is no way to call the stored handle. Following code gives error:
Error: Unable to perform assignment because dot indexing is not supported for variables of this type.
width=100;
app.a.b(app.index).Position(3)=width;

请先登录,再进行评论。

Instead of generating buttons, I think it would be better to populate a Drop Down with the names of the files, and open them that way, either on a selection changed callback, or with a separate button that opens the file based on the current selected value.
Generating buttons on the fly is generally never done for a reason.

2 个评论

I completely agree, but I have customers, and they don't like the extra mouse clicks associated with dropdowns.
However, how is generating them "on-the-fly" inside startupFcn any different from inside AppDesigner's automatically generated class constructor? They're the exact same MATLAB commands being executed one line a time either way.
functionally of course they'd be the same. The main issue for me is layout. I suppose if you know you'll never have more than a few buttons, it may not be a big deal. But what if you have 15? 33? or more? Obviously it could be problematic.
Also, how do you generate the callbacks for when the buttons are pressed?
I understand about customers and all, but this is just a really bad way to do it.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by