Save GUI editbox values to workspace variables

조회 수: 4 (최근 30일)
N/A
N/A 2016년 6월 26일
답변: Geoff Hayes 2016년 6월 27일
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

채택된 답변

N/A
N/A 2016년 6월 27일
  댓글 수: 2
Adam
Adam 2016년 6월 27일
I can't think of any reason why you would want variables in the main workspace rather than the GUI workspace - it hints at a confused design, but I guess if assignin does what you want on this occasion then that is fine.
Stephen23
Stephen23 2016년 6월 27일
편집: Stephen23 2016년 6월 27일
@rb250660: why not just read the MATLAB documentation?:
The method you found on that other thread is not particularly efficient, and has other disadvantages (such as copying over data, and making debugging very difficult) which make it a slow and buggy "solution".
Much better would be to learn how to pass data properly between workspaces (see the links above) and to learn to use data inside their own workspace, instead of wanting to see everything in the base workspace.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Geoff Hayes
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.

카테고리

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