Retrieve variables from inputdlg in a GUI

Hi,
I have built a GUI where the callback of 'Project Menu' has an input dialog as below code.
The GUI also has a bushbutton which requires the values which were input in the inputdlg.
Is there any way to retrieve the values of 'Project Name' and 'Project Number' to use in the bushbutton callback?
Thanks,
Prompt={'Project Name','Project Number'};
dlg_title='Project Info';
def= {' ',' '};
num_lines = [ones(size(def')) ones(size(def'))*75];
options.Resize='on';
options.Windowstyle='modal';
options.Interpreter='tex';
Project=inputdlg(Prompt,dlg_title,num_lines,def,options);

 採用された回答

Geoff Hayes
Geoff Hayes 2020 年 5 月 7 日

1 投票

Yasser - assuming that the user doesn't press cancel, then the project name and number can be retrieved from the cell array Project
projectName = Project{1};
projectNumberAsString = Project{2};

5 件のコメント

Yasser Eljajeh
Yasser Eljajeh 2020 年 5 月 7 日
Hi Geoff, Thanks for the answer. If the user presses "OK", I am not sure how we can recall these values for the bushbutton callback. When I use the syntax you recommended, I get an error saying "undefined variable". I need a way to store these values and use them in the bushbutton callback. Bear in mind that inputdlg is not treated as an independent GUI handles. Thanks,
Geoff Hayes
Geoff Hayes 2020 年 5 月 7 日
Yasser - isn't the above code in your pushbutton callback? Can you show me how/where this code is called? And please post the entier error message and code.
Yasser Eljajeh
Yasser Eljajeh 2020 年 5 月 7 日
Hi Geoff,
Please see below a snapshot from the GUI.
The Project info Menu (circled) result in the below dialog input box:
After pressing OK we need to use the Project Name and Project Number input in the "Generate Report" bushbutton.
When using the syntax you recommended in the "Generate Report" callback function, I am getting the below error:
I hope the problem is clear now and appreciate your help.
Best Regards,
Geoff Hayes
Geoff Hayes 2020 年 5 月 8 日
Yasser - so your input dialog is launched from a menu item? So your current menu button callback may look like
% --------------------------------------------------------------------
function projectinfo_Callback(hObject, eventdata, handles)
% hObject handle to projectinfo (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Prompt={'Project Name','Project Number'};
dlg_title='Project Info';
def= {' ',' '};
num_lines = [ones(size(def')) ones(size(def'))*75];
options.Resize='on';
options.Windowstyle='modal';
options.Interpreter='tex';
Project=inputdlg(Prompt,dlg_title,num_lines,def,options);
% <----- Add this code to save the output
if ~isempty(Project)
handles.projectName = Project{1};
handles.projectNumberAsString = Project{2};
else
% handle case where dialog is closed without inputs
handles.projectName = 'defaultProjectName';
handles.projectNumberAsString = 'defaultProjectNumber';
end
guidata(hObject, handles); % <----- saves the updated handles structure
So now the project information is saved to the handles structure. You can then access this data from your pushbutton callback as
if isfield(handles, 'projectName')
projectName = handles.projectName;
projectNumberAsString = handles.projectNumberAsString;
% do something
end
Yasser Eljajeh
Yasser Eljajeh 2020 年 5 月 8 日
Works perfectly.
Thanks Geoff,

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by