Storing Information in GUI using One Callback for Multiple Text Entries?
显示 更早的评论
I am working on designing a GUI for an engineering design program. I am attempting to use the MATLAB classes directly; thus, GUIDE is not used. This program is still in very early stages (proof of concept really at this point), but it is heavily dependent on user-based input and contains a number of text fields for this input. Each field is to contain a unique variable which can then be used elsewhere in the program.
Since each field stores a user-input value, it seems most efficient to use the same callback function (which simply stores a value) for each field; however, although I suspect there is a very simple solution, I'm having difficulty determining how best to achieve this result. Is this actually possible or is it necessary to use a unique callback function for each uicontrol, even if the functions are identical save for the variable being stored?
回答(1 个)
Jan
2017-6-1
You can use the same callback, of course. Callbacks are functions and they can be called from anywhere.
You hjave to decide, how the variables can be distinguished. The tag of the edit field can help here:
function mainGUI
FigH = figure;
handles.editA = uicontrol('Style', 'edit', 'Position', 10, 10, 100, 20, ...
'Tag', 'A', 'Callback', @EditCB);
handles.editB = uicontrol('Style', 'edit', 'Position', 10, 40, 100, 20, ...
'Tag', 'B', 'Callback', @EditCB);
handles.values.A = NaN;
handles.values.B = NaN;
guidata(FigH, handles);
end
function EditCB(EditH, EventData)
handles = guidata(EditH);
handles.values.(EditH.Tag) = str2double(EditH.String);
guidata(EditH, handles);
end
Now the value is stored in the handles struct. This is redundant, because you have the values in the edit uicontrols already. Perhaps it is not required to store the variables directly, but to obtain the values dynamically from all edit fields, when the computation is started.
类别
在 帮助中心 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!