Passing two subfunction variables in GUI to another function

Good Afternoon All,
I have been searching matlab documentation and forums for a way to properly pass data from a subfunction to another function in a GUI. I have only found examples for one variable in a subfunction and I am not sure if the handles will work for two considering every attemp made the way I defined the handles only exist within the subfunction.
% --- Executes on button press in Bore_Profile_Generate_Pushbutton.
function Bore_Profile_Generate_Pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to Bore_Profile_Generate_Pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Do simple equations and produce:
%Output results
angles = (prof(:,1))';
radius = (prof(:,2))';
%Then I try to use handles
handles.radius=radius;
handles.angles=angles;
Now I try to place it into a different function and fails.
% --- Executes on button press in Results_Analysis_Pushbutton.
function Results_Analysis_Pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to Results_Analysis_Pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Running fft_coeff.m to obtain the Fourier Coefficients of Profile
%Radius and Angles are obtained from Bore_Profile_Generate_Pushbutton and
%N_terms are obtained from Results_Coefficients_Edit
radius = str2num(get(handles.radius));
angles = str2num(get(handles.angles));
Note that radius and angles are a 1 by n matrix.

 Accepted Answer

You'll have to store the new handles structure at the end of the pushbutton callback using:
guidata(handles.figure1,handles);
Where 'figure1' is the tag of your figure, whatever that may be.

10 Comments

Thanks so much for your response. I tried and received the following error after running the file:
>> Bore_Distortion_Calculator Reference to non-existent field 'Bore_Distortion_Calculator'.
Error in Bore_Distortion_Calculator>Bore_Profile_Generate_Pushbutton_Callback (line 289) guidata(handles.Bore_Distortion_Calculator,handles);
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in Bore_Distortion_Calculator (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)Bore_Distortion_Calculator('Bore_Profile_Generate_Pushbutton_Callback',hObject,eventdata,guidata(hObject))
Bore_Distortion_Calculator is the figure 'Tag'?
If you put a breakpoint on that line, push the button - then inspect the contents (fields) of the handles structure, is Bore_Distortion_Calculator a field of it?
Again thanks for the help. It is the m file name so I was assuming that it is the tag name as well. I do not see Bore_Distortion_Calculator in the openfunction structure when I retrace things. Should I have used Bore_Distortion_Function_Output instead to add the handles?
No, it won't be necessarily. Open the figure in GUIDE, right click on the figure somewhere to open the property inspector and look for the 'Tag' property. Default is 'figure1' I believe.
Oh ha wow I did not know there was a tag for that. Thanks, I changed the tag and reran it only to receive the same error.
Did you save AND close everything and then run it again?
If so, then put the break point on that line and see what the fields of handles are...
You were correct again. I have that to work now when trying to get the data I receive the following errors:
Error using handle.handle/get
Invalid or deleted object.
Error in Bore_Distortion_Calculator>Results_Analysis_Pushbutton_Callback (line 110)
radius = get(handles.radius);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in Bore_Distortion_Calculator (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)Bore_Distortion_Calculator('Results_Analysis_Pushbutton_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
OMG it worked!!!!!!!!!!!!!!!
Thank you so much for your patience and time in assisting me.

Sign in to comment.

More Answers (1)

You add to handles, but then do not save handles. See the help for GUIDATA.

2 Comments

Thank you so much for the response. I referenced teh GUIdata and typed in the following but still received an error.
%Output results
angles = (prof(:,1))';
radius = (prof(:,2))';
%Declaring output results to be used for Distortion Results
handles.radius=radius;
handles.angles=angles;
%Saving Handles to be used.
guidata(handles.Bore_Distortion_Calculator,handles)
Use this instead:
guidata(gcbf,handles); % No need to mess around with tags...
And if you still receive an error, show the error message!

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!