john.steak
john.steak

Reputation: 59

Plotting in an axes from a function

I am working on a GUI in MATLAB; I used the GUIDE in the command window. I have several pushbuttons. Now the problem, I have a function, in a pusbhbutton4 and when I click on it I want to plot the result in three specific axes (10 - 12). But does not work.

The code:

function pointsQRS = MyCustomPushButtonFunctionQRS10(VxQRS, VyQRS, VzQRS)
VxQRS=[[0:length(VxQRS)-1]' VxQRS];
axes(handles.axes10);
plot(VxQRS(:,2));
grid on 
Vx_QRS=ginput; 
x_pointsQRS=VxQRS(Vx_QRS(1,1)<=VxQRS(:,1) &  Vx_QRS(2,1)>=VxQRS(:,1),:);
m=1;


VyQRS=[[0:length(VyQRS)-1]' VyQRS];
axes(handles.axes11);
plot(VyQRS(:,2));
grid on
Vy_QRS=ginput; 
y_pointsQRS=VyQRS(Vy_QRS(1,1)<=VyQRS(:,1) & Vy_QRS(2,1)>=VyQRS(:,1),:);
if size(y_pointsQRS,1)<m
 m=2;
 end


  VzQRS=[[0:length(VzQRS)-1]' VzQRS];
  axes(handles.axes12);
  plot(VzQRS(:,2));
  grid on

  Vz_QRS=ginput; 
  z_pointsQRS=VzQRS(Vz_QRS(1,1)<=VzQRS(:,1) & Vz_QRS(2,1)>=VzQRS(:,1),:);
  if size(z_pointsQRS,1)<m
    m=3;
  end



  switch m 
   case 1
    x_pointQRS=x_pointsQRS;                                                                   
    y_pointQRS=y_pointsQRS(x_pointsQRS(1,1)<=y_pointsQRS(:,1) &    x_pointsQRS(end,1)>=y_pointsQRS(:,1),:);
    z_pointQRS=z_pointsQRS(x_pointsQRS(1,1)<=z_pointsQRS(:,1) &  x_pointsQRS(end,1)>=z_pointsQRS(:,1),:);
case 2
    y_pointQRS=y_pointsQRS;
    x_pointQRS=x_pointsQRS(y_pointsQRS(1,1)<=x_pointsQRS(:,1) & y_pointsQRS(end,1)>=x_pointsQRS(:,1),:);
    z_pointQRS=x_pointsQRS(y_pointsQRS(1,1)<=z_pointsQRS(:,1) & y_pointsQRS(end,1)>=z_pointsQRS(:,1),:);
case 3
    z_pointQRS=z_pointsQRS;
    x_pointQRS=x_pointsQRS(z_pointsQRS(1,1)<=x_pointsQRS(:,1) & z_pointsQRS(end,1)>=x_pointsQRS(:,1),:);
    y_pointQRS=y_pointsQRS(z_pointsQRS(1,1)<=y_pointsQRS(:,1) & z_pointsQRS(end,1)>=y_pointsQRS(:,1),:);
 end


 size_min=min([size(x_pointQRS,1) size(y_pointQRS,1) size(z_pointQRS,1)]) 
 pointsQRS([1:size_min],:)=[x_pointQRS([1:size_min],2)        y_pointQRS([1:size_min],2) z_pointQRS([1:size_min],2)];
      if size_min==0
       error('Wrong.');
       end
      end

The pushbutton code:

% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, Data)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


pointsQRS = MyCustomPushButtonFunctionQRS10(Data.fileData(:,1), Data.fileData(:,2), Data.fileData(:,3))


save 'pointsQRS.mat' -mat pointsQRS

Error I'm still getting:

Undefined variable "handles" or class "handles.axes10".

Error in MyCustomPushButtonFunctionQRS10 (line 3)
axes(handles.axes10);

Error in VKG_Zobrazovac>pushbutton4_Callback (line 156)
pointsQRS = MyCustomPushButtonFunctionQRS10(Data.fileData(:,1), Data.fileData(:,2), Data.fileData(:,3))

Error in gui_mainfcn (line 95)
        feval(varargin{:});

Error in VKG_Zobrazovac (line 42)
    gui_mainfcn(gui_State, varargin{:});

Error in @(hObject,eventdata)VKG_Zobrazovac('pushbutton4_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating UIControl Callback

Could you please give me any hint how to make it work properly?

Upvotes: 1

Views: 76

Answers (1)

Suever
Suever

Reputation: 65430

You are not passing the handles struct to MyCustomPushButtonFunctionQRS10 (or as you call it, Data) so it has no way to access the axes handles stored in handles. You should pass handles as an input argument.

pointsQRS = MyCustomPushButtonFunctionQRS10(Data.fileData(:,1), ...
                                            Data.fileData(:,2), ...
                                            Data.fileData(:,3), ...
                                            Data)

And then your callback would accept a fourth input

function pointsQRS = MyCustomPushButtonFunctionQRS10(VxQRS, VyQRS, VzQRS, handles)

Additionally, I would recommend using the 'Parent' property of plot to specify the parent axes rather than using axes.

plot(VxQRS(:,2), 'Parent', handles.axes10);
grid(handles.axes10, 'on')
Vx_QRS = ginput(handles.axes10); 

Upvotes: 2

Related Questions