如何从GUIDE GUI返回自定义值而非图形句柄?
Alright, let's figure out how to make your GUIDE-based MATLAB GUI return a custom value instead of the default figure handle when you call it like value = myGui();. I've got a couple of solid methods for you, depending on how complex your GUI is:
OutputFcn + waitfor (Recommended) This is the most robust approach since it leverages GUIDE's native functionality to handle output values. Here's how to set it up:
Step 1: Modify the OpeningFcn to pause execution
In your GUI'sOpeningFcn, set the window to modal (optional but highly recommended to prevent users from interacting with other windows before finishing with your GUI) and usewaitforto hold the function until the window is closed:function myGui_OpeningFcn(hObject, eventdata, handles, varargin) % Optional: Make the GUI modal to force focus set(hObject, 'WindowStyle', 'modal'); % Initialize the custom output value (default to empty if needed) handles.output = []; guidata(hObject, handles); % Wait until the GUI window is destroyed (either by user close or your code) waitfor(hObject);Step 2: Update the output value in your action callback
When the user completes their task (e.g., clicking an "OK" button), capture the custom value you want to return, save it to thehandlesstructure, then close the GUI:function pushbutton_ok_Callback(hObject, eventdata, handles) % Example: Get input from an edit box and convert to a number user_input_str = get(handles.edit1, 'String'); custom_value = str2double(user_input_str); % Save the custom value to the handles structure handles.output = custom_value; guidata(hObject, handles); % Close the GUI to trigger the OutputFcn delete(handles.figure1);Step 3: Configure the OutputFcn to return your value
Modify the auto-generatedOutputFcnto return the custom value stored inhandlesinstead of the default figure handle:function varargout = myGui_OutputFcn(hObject, eventdata, handles) % Pass your custom value as the output varargout{1} = handles.output; % If you need multiple outputs, add more lines like: % varargout{2} = handles.another_value;
Now when you call value = myGui();, the GUI will open, wait for the user to click OK (or close it), then return your custom value instead of the figure handle.
If your GUI is straightforward and you don't want to mess with the OutputFcn, you can use setappdata and waitfor to track the custom value directly in the main GUI function:
function custom_value = myGui() % GUIDE's auto-generated setup code (keep this as-is) gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @myGui_OpeningFcn, ... 'gui_OutputFcn', @myGui_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end % Run the GUI setup gui_mainfcn(gui_State, varargin{:}); % Wait until we set a "done" flag in the window's UserData waitfor(gcf, 'UserData'); % Retrieve the custom value we stored custom_value = getappdata(gcf, 'custom_value'); % Clean up the window delete(gcf); end
Then in your OK button callback:
function pushbutton_ok_Callback(hObject, eventdata, handles) % Capture your custom value custom_value = str2double(get(handles.edit1, 'String')); % Store it in the window's app data setappdata(handles.figure1, 'custom_value', custom_value); % Set the "done" flag to trigger the waitfor in the main function set(handles.figure1, 'UserData', 'completed'); end
- Modal Windows: Setting
WindowStyle='modal'ensures the user can't interact with other MATLAB windows until they finish with your GUI, which prevents accidental early returns of the default figure handle. - Multiple Outputs: For Method 1, just add more entries to
varargoutin theOutputFcnand call the GUI with[val1, val2] = myGui();. - Avoid Early Returns: Make sure you only close the GUI or trigger the
waitforcondition after the custom value is fully ready—otherwise, you'll get the default figure handle instead of your desired value.
内容的提问来源于stack exchange,提问作者wgrant

