Save GUI editbox values to workspace variables
이전 댓글 표시
I am trying to save the values entered into my GUI edit boxes to variables. I am sure the answer is simple but after a lot of searching and looking at examples I still can't figure it out. Below is my example code.
Basically it is a pretty version of inputdlg however I cannot work out how to save the variables from the edit boxes. I will also need to save the state of a check box.
Other questions I have are: Is there a way to preload the edit boxes with data from an existing array? Is the code/functionality behind my OK and Cancel buttons as expected?
function Test_UI_forums
%%Create figure
% Figure
scrsz = get( groot, 'ScreenSize' );
figure( 'Name', 'My Figure',...
'Position', [scrsz(3)/2-215 scrsz(4)/2-165 430 320],...
'Resize', 'off',...
'NumberTitle','off',...
'Toolbar', 'none',...
'Menu', 'none' );
%%Edit boxes
% Value 1
h.value1 = uicontrol( 'Style', 'Edit',...
'String', '50',...
'HorizontalAlignment', 'Right',...
'Position', [20 265 60 25],...
'Callback', @value1_callback );
% Text
h.value1_text = uicontrol( 'Style', 'Text',...
'String', 'Value 1',...
'HorizontalAlignment', 'Left',...
'Position', [85 265 120 20] );
% Value 2
h.value2 = uicontrol( 'Style', 'Edit',...
'String', '5',...
'HorizontalAlignment', 'Right',...
'Position', [20 235 60 25],...
'Callback', @value2_callback );
% Text
h.value2_text = uicontrol( 'Style', 'Text',...
'String', 'Vaule 2',...
'HorizontalAlignment', 'Left',...
'Position', [85 235 120 20] );
% OK button
hBut_ok = uicontrol( 'style', 'pushbutton',...
'Position', [270 10 70 25],...
'String', 'OK',...
'Callback', @cBut_ok );
% Cancel button
hBut_cancel = uicontrol( 'style', 'pushbutton',...
'Position', [350 10 70 25],...
'String', 'Cancel',...
'Callback', @cBut_cancel );
uiwait;
end
%%Functions
% Value 1
function value1_callback( hObject, eventdata )
Value1 = str2double( get( hObject, 'String' ) );
if isnan( Value1 )
errordlg( 'You must enter a numeric value', 'Invalid input', 'Modal' );
return
else
display( Value1 );
end
end
% Value 2
function value2_callback( hObject, eventdata )
Value2 = str2double( get( hObject, 'String' ) );
if isnan( Value2 )
errordlg( 'You must enter a numeric value', 'Invalid input', 'Modal' );
return
else
display( Value2 );
end
end
% OK button
function cBut_ok( hObject, eventdata )
close( gcf );
end
% Cancel button
function cBut_cancel( hObject, eventdata )
delete( gcf );
end
채택된 답변
추가 답변 (1개)
Geoff Hayes
2016년 6월 27일
rb250660 - if you are trying to create a pretty version of inputdlg then you should try to match your function signature with theirs (see inputdlg for details). That would mean changing your signature to
function [answers] = Test_UI_forums
where answers is an array (perhaps a cell array) of strings or numeric values that the user has entered into the dialog. (The output answers could also be a struct.) You would then pair uiresume with uiwait to create your modal dialog that waits for the user to enter new values (or cancel) before returning the answers output to the workspace that called the function. See https://www.mathworks.com/matlabcentral/answers/92906-how-do-i-build-a-modal-dialog-box-gui-that-waits-for-input-from-a-user for an example of such a modal dialog.
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!