Info

This question is closed. Reopen it to edit or answer.

GUI script/function issue

1 view (last 30 days)
Krsto Sbutega
Krsto Sbutega on 25 Apr 2013
Closed: MATLAB Answer Bot on 20 Aug 2021
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

Answers (2)

Walter Roberson
Walter Roberson on 25 Apr 2013
Callbacks that are strings are evaluated in the base workspace. Your h2 and so on are not being defined in the base workspace: they are being defined in the workspace of Heat_Sink_UI

Krsto Sbutega
Krsto Sbutega on 25 Apr 2013
Walter, Thanks a lot for your answer. That makes sense but I did not know that. Thanks for the the link. I'll work on it and let you know how it goes. Krsto

Tags

Products

Community Treasure Hunt

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

Start Hunting!