Wait for a function to finish while the function runs GUI

Hello Everyone,
I have a function which works perfectly in itself:
function [ Main_Lang ] = Language( )
% select language page.
% runs the program in selected language.
Main_Lang = -2;
fig_1 = figure('name', 'fig_1');
% define flags for langauge selection, used for the callback function
% lang value 0 - Hebrew
% lang value 1 - English
% Hebrew button settings
lang_heb = uicontrol('style', 'pushbutton',...
'string', 'עברית',...
'fontsize', 12,...
'units','normalized',...
'position', [0.25 0.5 0.2 0.1],...
'callback', 'Main_Lang = 0; uiresume(fig_1)');
% English button settings
lang_eng = uicontrol('style', 'pushbutton',...
'fontsize', 12,...
'string', 'English',...
'units','normalized',...
'position', [0.55 0.5 0.2 0.1],...
'callback', 'Main_Lang = 1; uiresume(fig_1)');
uiwait(fig_1);
close fig_1;
end
I have two issues while running it from this Main script:
Num_Ques = 8;
[Main_Lang] = Language()
if Main_Lang == 0
Ques_arr = Choices_Heb()
end
Currently, for some reason when the function is opened by the Main, when it reached the point of the uiwait, I get this: Error using waitfor Undefined function or variable 'fig_1'.
Error using waitfor Error while evaluating UIControl Callback
While if I run the function on itself it works fine.
What I'm trying to accomplish is the the Main script will not continue until the Language function is done (since I immediately use the value I get).
Can you please advise?
Thank you!
Jasmine

 Accepted Answer

a) This does not run if you call the function by itself. b) Your issue has to do with the callback functions of the uicontrol. Functions maintain separate workspaces and therefore fig_1 is not in the scope of the callback functions and the Main_Lang in the callback is different from the one in the Language() scope. Consider making your callbacks for the function as such:
function cBack(src,evt,mainLang,fig)
% Callbacks do not allow output variables, but you can store data in the figure itself.
set(fig,'UserData',mainLang);
% fig.UserData = mainLang; %Post 2014b syntax
uiresume(fig);
end
Now callbacks expect only the first 2 unused inputs, so make an anonymous funtion:
callBackHebrew = @(src,evt) cBack(src,evt,0,fig)
callBackEnglish = @(src,evt) cBack(src,evt,1,fig)
Now just make sure you extract the language identifier from the figure inside of the function Language()
uiwait(fig_1);
Main_Lang = get(fig_1,'UserData');
% Main_Lang = fig_1.UserData; % Post 2014b syntax
close fig_1;

7 Comments

Thank you! I'll try that!
I think I don't quite understand something though. If the function returns Main_Lang, which is a simple variable in the function, why is fig_1 required by the Main?
Also, for some reason when I tried to name the variable in the main and in the function differently - it didn't work..
Thanks again so much!
I'm not quite sure where the confusion is on point 1. When you say "the Main" I am not sure how to interpret. Do you mean the function Language? If so, this is necessary so that you can pass data to the figure which is calculated in the callback function as callbacks don't allow outputs.
On the second point that is a slight mistake. This should of course have the anonymous functions as:
callBackHebrew = @(src,evt) cBack(src,evt,0,fig_1)
callBackEnglish = @(src,evt) cBack(src,evt,1,fig_1)
as we are passing the figure from the function Language to the callback functions.
Got it on the second one :)
About the first point, I mean the script which calls the function. If I run simply:
Lang = Language()
And the function Language treats the variable Main_Lang as a regular variable, then why do I get an error (in the command line) regarding variable fig_1 unrecognized? I mean, I just need the output of the function, and it IS recognized INSIDE the function.
Sorry for bothering you with this, I would just really like to understand what's going on "behind the scenes" of this thing.
Everything seems to run fine for me with this. I have attached the file with the Language function and I run the command (first selecting English and then again selecting Hebrew:
Main_Lang = Language() % Selecting English
Main_Lang =
1
Main_Lang = Language() % Selecting Hebrew
Main_Lang =
0
It is possible you made an error in combining all of the code together (see mine for reference) The only thing that I could imagine is that the error is happening inside the other function you have, Choices_Heb(). Does this function use the figure? If this does not resolve it, please place the whole error message received.
This is what I tried:
>> Main_Lang = Language() %Selecting Hebrew
Error using waitfor
Undefined function or variable 'fig_1'.
Error using waitfor
Error while evaluating UIControl Callback
I would suggest then comparing your code to what I submitted in my previous post. It seems that likely something was done in the wrong order in your file. You can always set breakpoints in your code to try and discern the exact source as well.
I placed it here as code for ease of viewing:
function [ Main_Lang ] = Language( )
% select language page.
% runs the program in selected language.
Main_Lang = -2;
fig_1 = figure('name', 'fig_1');
% define flags for langauge selection, used for the callback function
% lang value 0 - Hebrew
% lang value 1 - English
% Hebrew button settings
callBackHebrew = @(src,evt) cBack(src,evt,0,fig_1);
callBackEnglish = @(src,evt) cBack(src,evt,1,fig_1);
lang_heb = uicontrol('style', 'pushbutton',...
'string', '?????',...
'fontsize', 12,...
'units','normalized',...
'position', [0.25 0.5 0.2 0.1],...
'callback', callBackHebrew);
% English button settings
lang_eng = uicontrol('style', 'pushbutton',...
'fontsize', 12,...
'string', 'English',...
'units','normalized',...
'position', [0.55 0.5 0.2 0.1],...
'callback', callBackEnglish);
uiwait(fig_1);
Main_Lang = get(fig_1,'UserData');
% Main_Lang = fig_1.UserData; % Post 2014b syntax
close fig_1;
end
function cBack(src,evt,mainLang,fig)
% Callbacks do not allow output variables,
% but you can store data in the figure itself.
set(fig,'UserData',mainLang);
% fig.UserData = mainLang; %Post 2014b syntax
uiresume(fig);
end
Ohhhh MAGIC!
Thank you a thousand times! I now understand your previous explanation also, Thanks again!

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!