How to control GUI with external m-file
Show older comments
I created a GUI, named 'StartEngine', there is a button 'Run'(handles.pushbutton1) on the GUI.
Now, I opened the GUI and I want to make Background color of this button to be gray with a m-file.
It means how could I implement the following command out of StartEngine.m (corresponding to StartEnge.fig)
set(handles.pushbutton1, 'BackgroundColor', [0.5, 0.5, 0.5]);
11 Comments
Andreas Bernatzky
on 13 Dec 2019
Hey Leo,
Are you using guide or appdesigner?
I can just make some assumptions for now and try it later for myself...
But my solution for now:
if the gui is already open you can acess the app. Object which gets created at startup of your gui. Now you can change the behaviour/color of the buttons.
Best Regards
Andi
Adam Danz
on 13 Dec 2019
"Now, I opened the GUI and I want to make Background color of this button to be gray with a m-file."
If you're referring to the m-file that contains the GUI, you would simply execute the line of code you shared in your question,
set(handles.pushbutton1, 'BackgroundColor', [0.5, 0.5, 0.5]);
If you're taking about an external m-file that does not host the GUI, I urge you not to use this approach. External files should not be making these kinds of changes to a GUI
Adam Danz
on 16 Dec 2019
The [Run] button should have a callback function and your ExecuteRun.m should be called from within that callback function. That callback function should also control the background color of [Done].
Walter Roberson
on 16 Dec 2019
It is incorrect to say that code executed from the base workspace will not affect the GUI. The only reason that could would fail would be if handles does not exist or handles does not have a pushbutton2 field, or it does but it refers to something that does not have a BackgroundColor to set.
The step that is probably missing for you is
fig = findobj('type', 'figure', 'Name', 'StartEngine');
handles = guidata(fig);
Mohammad Sami
on 16 Dec 2019
If you have the latest version of Matlab, you may want to build your GUI in appdesigner instead.
You can then right click the Run Button in appdesigner and add a callback function. Inside your callback you can execute any code you wish. You can easily access the run button and change its color in the callback. The button would be a property in the app class automatically created by appdesigner.
Leo Zhai
on 18 Dec 2019
Walter Roberson
on 18 Dec 2019
You could try findall() instead of findobj()
What shows up if you do
figs = findall('type', 'figure');
get(figs, 'Name')
get(figs, 'Tag')
Leo Zhai
on 18 Dec 2019
Walter Roberson
on 18 Dec 2019
figs = findall(0, 'type', 'figure');
get(figs, 'Name')
get(figs, 'Tag')
Accepted Answer
More Answers (0)
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



