필터 지우기
필터 지우기

Creating Variables with GUI inputs

조회 수: 32 (최근 30일)
Erick Magana
Erick Magana 2019년 11월 13일
댓글: Erick Magana 2019년 11월 14일
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