Return variable from nested function

11 次查看(过去 30 天)
I want to create a dropdown menu from a list of cities. I want to select a city, and press a button that will close the figure and return the selection to the global scope. I'm using nested functions to do this. I'm unsuccessful at the moment as I get the error:
Function return value may be unset.
When I run the script, it doesn't work and I get the error (after I press the "submit" button):
Output argument "city" (and possibly others) not assigned a value in the execution
with "test>select_city" function.
Error in test (line 3)
city = select_city(city_list);
This is what I'm trying:
city_list = {'Paris', 'London', 'Tokyo', 'Washington'};
city = select_city(city_list);
function city = select_city(city_list)
% Create a GUI to set the city name
fig = uifigure;
fig.Units = 'pixels';
fig.Name = 'Select City';
fig.Position = [500, 500, 300, 200];
% Add the label to prompt user to select the city
select_city_header = uilabel(fig);
select_city_header.Text = 'Select City';
select_city_header.FontSize = 18;
select_city_header.Position = [70, 100, 1000, 50];
% create dropdown menu to select the city
dd = uidropdown(fig,'Items', city_list, 'Value',city_list{1}, 'Position', [75, 75, 100, 22]);
% create a button to close the figure and get the data
btn = uibutton(fig, 'ButtonPushedFcn', @(btn,event) select_city());
btn.Position = [75, 40, 100, 22];
btn.Text = 'Submit';
function city = select_city()
city = dd.Value;
close(fig)
end
end

采纳的回答

Stephen23
Stephen23 2023-1-22
编辑:Stephen23 2023-1-22
Callback functions do not return output arguments**, so your approach will not work. The MATLAB documentation explains approaches for sharing data between callback functions:
You already decided to use a nested function, so you can simply use the parent workspace to share that argument (which is rather the point of nested functions). I modifed and tested your function, the main changes are:
  1. adding the CITY variable to the parent workspace so that it is shared by both the parent and nested function, as described here: https://www.mathworks.com/help/matlab/matlab_prog/nested-functions.html#f4-73993
  2. adding WAITFOR() so that the parent function waits for the user to close the figure (otherwise you parent function simply returns as soon as its code has run... not much point in that).
  3. for clarity renaming and simplifiying the nested function handle.
function city = select_city(city_list)
city = []; % !!!! the shared variable must be defined in the parent workspace !!!!
% Create a GUI to set the city name
fig = uifigure;
fig.Units = 'pixels';
fig.Name = 'Select City';
fig.Position = [500, 500, 300, 200];
% Add the label to prompt user to select the city
select_city_header = uilabel(fig);
select_city_header.Text = 'Select City';
select_city_header.FontSize = 18;
select_city_header.Position = [70, 100, 1000, 50];
% create dropdown menu to select the city
dd = uidropdown(fig,'Items', city_list, 'Value',city_list{1}, 'Position', [75, 75, 100, 22]);
% create a button to close the figure and get the data
btn = uibutton(fig, 'ButtonPushedFcn',@mycallback);
btn.Position = [75, 40, 100, 22];
btn.Text = 'Submit';
waitfor(fig) % !!!! you need WAITFOR() !!!!
function mycallback(~,~) % !!!! no point in any output arguments here !!!!
city = dd.Value;
close(fig)
end
end
Call it exactly as you showed:
>> city_list = {'Paris', 'London', 'Tokyo', 'Washington'};
>> city = select_city(city_list)
city =
'Tokyo'
** more precisely: the function may be defined with output arguments, but when the callback is triggered/called it is called without any outputs.
  2 个评论
Nev Pires
Nev Pires 2023-1-22
移动:Stephen23 2023-1-22
@Stephen23, thank you! This works perfectly. What does the ~'s mean in the mycallback function?
Stephen23
Stephen23 2023-1-22
编辑:Stephen23 2023-1-22
"What does the ~'s mean in the mycallback function? "
Callback functions must accept atleast two input arguments:
but they can be ignored because your function does not use them:

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by