필터 지우기
필터 지우기

How to change color of GUI window from Matlab command line

조회 수: 8 (최근 30일)
KAE
KAE 2018년 2월 16일
댓글: Rik 2019년 12월 10일
Let's say I have a Matlab program that generates a GUI to display some results, and I want to change the background color of the GUI from the Matlab command line. Assume I don't know the GUI window tag name and don't already have access to its handle.
Below is an example GUI from Matt Fig. Once the code is run, what command line commands will turn the resulting GUI yellow?
function [] = GUI_1()
% Demonstrate how to delete an entry from a uicontrol string.
% Creates a listbox with some strings in it and a pushbutton. When user
% pushes the pushbutton, the selected entry in the listbox will be deleted.
%
% Suggested exercise: Modify the GUI so when the user deletes a certain
% string, the 'value' property is set to the previous string instead of to
% the first string.
%
%
% Author: Matt Fig
% Date: 7/15/2009
S.fh = figure('units','pixels',...
'position',[500 500 200 260],...
'menubar','none',...
'name','GUI_1',...
'numbertitle','off',...
'resize','off');
S.ls = uicontrol('style','list',...
'unit','pix',...
'position',[10 60 180 180],...
'min',0,'max',2,...
'fontsize',14,...
'string',{'one';'two';'three';'four'});
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[10 10 180 40],...
'fontsize',14,...
'string','Delete String',...
'callback',{@pb_call,S});
function [] = pb_call(varargin)
% Callback for pushbutton, deletes one line from listbox.
S = varargin{3}; % Get the structure.
L = get(S.ls,{'string','value'}); % Get the users choice.
% We need to make sure we don't try to assign an empty string.
if ~isempty(L{1})
L{1}(L{2}(:)) = []; % Delete the selected strings.
set(S.ls,'string',L{1},'val',1) % Set the new string.
end

채택된 답변

Geoff Hayes
Geoff Hayes 2018년 2월 16일
KAE - you might be able to use gcf (to get the handle to the current figure) if we assume that your figure/GUI has focus. Then change it's colour as
set(gcf,'Color',[1 1 0])
  댓글 수: 3
Steven Lord
Steven Lord 2018년 2월 16일
Most of the time, I would expect a figure opened by a GUI will have its HandleVisiblity property set to 'callback' or 'off'. The gcf function can only "find" the current figure if its HandleVisibility is 'on'.
To find the handle of a figure whose HandleVisiblity is set to 'callback' or 'off' use the findall function. Figures are children ("in the hierarchy", to use the phrasing on the findall page) of groot (if you're using release R2014b or later) or 0 (for releases prior to release R2014b.)
findall(groot, 'Type', 'figure')
KAE
KAE 2018년 2월 16일
Steven - That must have been the case, since the following works as desired:
h = findall(groot, 'Type', 'figure'); % Returns handle to the GUI
set(h, 'color', 'y');
If you turn this into an answer I will accept it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Eric Alexander
Eric Alexander 2019년 12월 10일
i believe that in order for you to use set(gcf....) from the command window you need to set your figure's handlevisibility property to 'on'; This can be done when you first create the figure:
S.fh = figure('units','pixels',...
'position',[500 500 200 260],...
'menubar','none',...
'name','GUI_1',...
'numbertitle','off',...
'HandleVisibility','on',...
'resize','off');
Another note you should make is that the structure S that stores all of your variables is only accessible from inside the function GUI_1 and not local to the matlab workspace. You can send this structure to the workspace by running the command assignin('base','S',S) from inside the function on startup. This allows you to manipulate the GUI from the command window. Hope this helps!
  댓글 수: 1
Rik
Rik 2019년 12월 10일
You don't need the cludge of using asignin. Simply returning S as an output of the function will be enough.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by