Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
GUI script/function issue

Subject: GUI script/function issue

From: Krsto Sbutega

Date: 25 Apr, 2013 20:15:09

Message: 1 of 3

Hello everyone,
I am having some problems with the creation of a GUI. I am relatively new to GUI so I am having a problem that I cannot figure out. Here it is.
I created a GUI in which you input some parameters and when you press next it stores them and moves to the next window. It all worked fine when I had it in my script, but now I am trying to make my code modular and I want to have the GUI as a separate function. However, when I move it to a function, I get an error because my handle numbers for some reason are not passed to the callback function when the user presses next. I am not sure what the issue is and I don't know how to resolve it.
A little sample code is given here:

function UiData = Heat_Sink_UI
close all;
%Create Figure

h = figure('Name','Geometry Selection','NumberTitle','off');

uicontrol(h,'Style', 'text','String', 'Thermal Inputs' ,'Position', [20 390 520 20],'FontWeight','bold','FontSize',11);

% Inlet temperature in text format.

uicontrol(h,'Style', 'text','String', 'Inlet Temperature [K]: ' ,'Position', [150 340 260 30],'FontWeight','bold','FontSize',11);
h2=uicontrol(h,'Style', 'edit','String', '23' ,'Position', [150 300 260 30],'FontSize',11);

% Heat to be dissipated.

uicontrol(h,'Style', 'text','String', 'Heat Load [W]: ','Position', [150 240 260 30],'FontWeight','bold','FontSize',11);
h3=uicontrol(h,'Style', 'edit','String', '300','Position', [150 200 260 30],'FontSize',11);

% Save values in array Therm and move forward.

hc = uicontrol('Style', 'pushbutton','String', 'Next','Position', [320 20 200 20],'FontSize',11);

set(hc,'Callback','Therm=Store_Move_to_Next([h2 h3],h,1,[1 1]);')

uiwait(h)

clf(h)

function [val] = Store_Move_to_Next(hObject,h,l,k)
    
n = length(hObject);

for i=1:n;
    
    if k(i)==1
        
        %For text edit.
        
        val.text(:,i) = str2double(get(hObject(i),'String'));
        
    elseif k(i)==2;
        
        %For listbox.
        
        val.list(:,i) = (get(hObject(i),'Value'))';
        
    end
    
    if l == 1;
        
        uiresume(h)
        
    else
        
    end
   
end

The code runs totally fine when I run it as a script but when I run it as a function I'll get something like:

Error using waitfor
Undefined function or variable 'h2'.
 
Error using waitfor
Error while evaluating uicontrol Callback

but h2, h3 and h are defined. Again, it runs fine as a script but when I run it as a function it does not pass the figure (or uicontrol) handles to the callback functions.
Thank you in advance for your help,

Krsto

Subject: GUI script/function issue

From: CĂ©ldor

Date: 25 Apr, 2013 22:04:07

Message: 2 of 3

I have slightly changed your code and it works now but I don't know whether it does what it's supposed to. I changed callback setting to using Handle function; it is more suggested method than the one you used. In general, when you define callback function, the first argument stores a handle to the object itself. A second argument stores eventdata passed to the callback functions, for instance which key has been pressed if it's KeyPressFcn callback. The rest of arguments are yours but you need to define them when callback is set.

I hope it helps.

This is the code:

[code]
function UiData = test_file_from_internet

    close all;

    %Create Figure
    h = figure('Name','Geometry Selection','NumberTitle','off');
    uicontrol(h,'Style', 'text','String', 'Thermal Inputs' ,'Position', [20 390 520 20],'FontWeight','bold','FontSize',11);

    % Inlet temperature in text format.
    uicontrol(h,'Style', 'text','String', 'Inlet Temperature [K]: ' ,'Position', [150 340 260 30],'FontWeight','bold','FontSize',11);
    h2=uicontrol(h,'Style', 'edit','String', '23' ,'Position', [150 300 260 30],'FontSize',11);

    % Heat to be dissipated.
    uicontrol(h,'Style', 'text','String', 'Heat Load [W]: ','Position', [150 240 260 30],'FontWeight','bold','FontSize',11);
    h3=uicontrol(h,'Style', 'edit','String', '300','Position', [150 200 260 30],'FontSize',11);

    % Save values in array Therm and move forward.
    hc = uicontrol('Style', 'pushbutton','String', 'Next','Position', [320 20 200 20],'FontSize',11);
    set(hc,'Callback',{@Store_Move_to_Next, [h2 h3],h,1,[1 1]})

    uiwait(h)
    clf(h)
end

function [val] = Store_Move_to_Next(hObject,eventdata, hMatrix, h,l,k)
    n = length(hMatrix);
    for i=1:n;
        if k(i)==1
            %For text edit.
            val.text(:,i) = str2double(get(hMatrix(i),'String'));
        elseif k(i)==2;
            %For listbox.
            val.list(:,i) = (get(hMatrix(i),'Value'))';
        end
        if l == 1;
            uiresume(h)
        end
    end
end
[/code]

Subject: GUI script/function issue

From: Steven_Lord

Date: 26 Apr, 2013 13:19:03

Message: 3 of 3



"Krsto Sbutega" <ksbutega@hotmail.com> wrote in message
news:klc2sd$j52$1@newscl01ah.mathworks.com...
> Hello everyone,
> I am having some problems with the creation of a GUI. I am relatively new
> to GUI so I am having a problem that I cannot figure out. Here it is.
> I created a GUI in which you input some parameters and when you press next
> it stores them and moves to the next window. It all worked fine when I had
> it in my script, but now I am trying to make my code modular and I want to
> have the GUI as a separate function. However, when I move it to a
> function, I get an error because my handle numbers for some reason are not
> passed to the callback function when the user presses next. I am not sure
> what the issue is and I don't know how to resolve it. A little sample code
> is given here:

Walter already answered this over on MATLAB Answers:

http://www.mathworks.com/matlabcentral/answers/73612-gui-script-function-issue

but I'd like to chime in with one point. While specifying callbacks as
strings is _allowed_ (for backwards compatibility with pre-function handle
releases, I believe) I encourage you NOT to specify them as strings unless
the callback is something extremely simple, like a DISP statement. Specify
them as a function handle (which could be an anonymous function) instead.

*snip*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us