Wait for a function to finish while the function runs GUI

8 vues (au cours des 30 derniers jours)
Jasmine Shragai
Jasmine Shragai le 11 Août 2015
Commenté : Jasmine Shragai le 13 Août 2015
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

Réponse acceptée

Brendan Hamm
Brendan Hamm le 12 Août 2015
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 commentaires
Brendan Hamm
Brendan Hamm le 13 Août 2015
Modifié(e) : Brendan Hamm le 13 Août 2015
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
Jasmine Shragai
Jasmine Shragai le 13 Août 2015
Ohhhh MAGIC!
Thank you a thousand times! I now understand your previous explanation also, Thanks again!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by