MATLAB Answers

How to plot in GUI from obtained values?

1 view (last 30 days)
Mary Caroline
Mary Caroline on 31 Oct 2013
Answered: Mary Caroline on 4 Nov 2013
Hello,
I have a problem that I have been trying to solve but I run out of ideas.
The programm is as followed: I input two data, which gives the directory to xls files. Those files are read, and the values they contain as well. Those values undergo several calculation to give an output vector. My goal is to plot this output vector. If I do it by hand: no problem. But what I tried to create is a GUI interface to make more user friendly. The computation does not have any problem. I have a button that launches is. It works.
But then I have another button to plot the result but I cannot get it to work. The axis are present, but Matlab does not understand which value I want to plot. My first idea was to read the data from the file created contaning the output vector. Given that it s a file created by matlab, it is quiet tricky to extract the values - it does not recognise any extension I put on it. So I decided to keep the output values as a vector inside matlab. But I don't know how to link the vector that is present in the function for the computation push button to the plot function button.
I tried the following way:
(TF_CMB and TF_B are the data I am trying to plot versus the frequence)
In the computation function:
handles.TF_CMB=TF_CMB;
handles.TF_B=TF_B;
handles.output=hObject;
guidata(hObject, handles);
And then in the plot function
TF_CMB = get(handles.button_start);
TF_B = get(handles.button_start);
freq=[250,315,400,500,630,800,1000,1200,1600,2000,2500,3150,4000,5000,6300,8000,10000];
plot(handles.axes1_2D,freq,TF_CMB,'r')
hold on
plot(handles.axes1_2D,freq,TF_B)
hold off
This is error that comes from it:
_??? Error using ==> plot
Conversion to double from struct is not possible.
Error in ==> cmb>pushbutton5_Callback at 183
plot(handles.axes1_2D,freq,TF_CMB,'r')
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> cmb at 20
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)cmb('pushbutton5_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback_
Any ideas ?

Answers (3)

Johannes Korsawe
Johannes Korsawe on 31 Oct 2013
Use the handles structure and the built-in function guidata:
function xls_read_button_Callback(hObject, eventdata, handles)
...
(some XLS read)
...
handles.TF_CMB=TF_CMB;
guidata(hObject,handles); % This is the important line
function data_plot_button_Callback(hObject, eventdata, handles)
TF_CMB=handles.TF_CMB; % The vector data of TF_CMB is available inside this function now
plot(TF_CMB);
Finished.

Mary Caroline
Mary Caroline on 4 Nov 2013
Thanks for your answer. I typed in exactly the same but it still doesn't work. Here is my exact script for the functions concerned:
function button_start_Callback(hObject, eventdata, handles)
% hObject handle to button_start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
rpm_init=str2double(get(handles.input_initial,'String'));
rpm_fin=str2double(get(handles.input_final,'String'));
interv=str2double(get(handles.input_intervalle,'String'));
[TF_CMB,TF_Banger]=CMB_function(rpm_init,interv,rpm_fin)
handles.TF_CMB=TF_CMB;
guidata(hObject,handles);
function axes1_2D_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1_2D (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
function pushbutton_plot_Callback(hObject, eventdata, handles)
TF_CMB=handles.TF_CMB;
plot(TF_CMB)
and the error I receive from Matlab:
??? Reference to non-existent field 'TF_CMB'.
Error in ==> cmb>pushbutton5_Callback at 174
plot(handles.TF_CMB)
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> cmb at 20
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)cmb('pushbutton5_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
I have looked up the help for guidata, and don't get why it does not work. Given that I didn't put a ";" after the call of my main function CMB_function, I see the ouputs of the function appear in the control window. So I know the problem does not come from the data but just from the way of storing it and transfering it from one function to another one.

Mary Caroline
Mary Caroline on 4 Nov 2013
Never mind, I managed to find the error. Thanks for the help Johannes !

Community Treasure Hunt

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

Start Hunting!