Save GUI editbox values to workspace variables

18 次查看(过去 30 天)
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

采纳的回答

rb250660
rb250660 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