Return variable from nested function
显示 更早的评论
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
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Exploration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!