Creating Variables with GUI inputs

34 次查看(过去 30 天)
I am trying to create a GUI with matlab and in order to make it work properly I need to ask for user input. I want to store these inputs in a variable and use them in the main part of my code. I have done some research but only found out that I could not create variables in a function and call them in an other fucntion. Is there anyway to do this? If so how would i incorperate it into my code?
  4 个评论
Ruger28
Ruger28 2019-11-13
Are you using GUIDE or creating one from scratch? I make a lot of GUIs, and use GUIDE mostly. This is easy to make an interface where a user has an input window, and you can press a button to "send" the data in the GUI to the main function.
Example using GUIDE: GUI with a simple edit field and a pushbutton (This is just the core of sending the variable from your edit field into your main program. You still need to create the GUIDE app.)
% --- Executes on button press in pushbuttonSEND.
function pushbuttonSEND_Callback(hObject, eventdata, handles)
user_input = get(handles.edit1,'String'); % gets the user input
% for if the string is a number, uncomment below and comment above
% user_input = str2num(get(handles.edit1,'String')); % gets the user input for number
[main_function_output] = MainFunction(user_input);
end
Erick Magana
Erick Magana 2019-11-14
Thanks, this all worked and gave me good insight on how Matlab works

请先登录,再进行评论。

回答(1 个)

Thomas Satterly
Thomas Satterly 2019-11-13
There are a number of ways to store data in a GUI so that it's accessible to different functions. Matlab's suggested way of passing data around a GUI is by using the setappdata and getappdata functions. Ex:
fh = figure;
inputField = uicontrol('Style', 'edit', ...
'Units', 'normalized', 'Position', [0.1 0.1 0.2 0.1], ...
'Callback', @(src, event) inputNewValue(src, event, fh));
label = uicontrol('Style', 'text', 'String', '', ...
'Units', 'normalized', 'Position', [0.7 0.1 0.2 0.1], ...
'BackgroundColor', [0.9 0.9 0.9]);
button = uicontrol('Style', 'pushbutton', 'String', 'Copy Input', ...
'Units', 'normalized', 'Position', [0.4 0.1 0.2 0.1], ...
'Callback', @(src, event) buttonPressCallback(src, event, fh, label));
function inputNewValue(src, event, handle)
setappdata(handle, 'InputString', src.String);
end
function buttonPressCallback(src, event, handle, output)
data = getappdata(handle, 'InputString');
if isempty(data)
output.String = '(No Input)';
else
output.String = data;
end
end
setappdata and getappdata work on any graphics object, not just figures, so it's possible to have multiple graphics objects with the same functionality (like multiple tabs) that can each store their own data in the same manner, allowing for some nice code re-use. It's also a good practice to initialize all the app data you need, that way you won't be risking empty values being returned from getappdata
There are other methods for passing around data, as noted by Stephen's comment, that are worth investigating as they may be a better fit for your use case.

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by