Question

I have created a GUI with 3 axes: axes1, axes2, axes3. I have a class SP to whose constructor I pass the three axes as follows:

a=SP(handles.axes1,handles.axes2,handles.axes3)

The class looks something like

class SP < handles
   properties
      axes1
      axes2
      axes3
   end
   methods
      function A=SP(axes1,axes2,axes3)
          A.axes1=axes1;
          A.axes2=axes2;
          A.axes3=axes3;
          axes(A.axes1);
          rectangle('Position',[randn,randn,randn,randn]);
          axes(A.axes2);
          rectangle('Position',[randn,randn,randn,randn]);
          axes(A.axes3);
          rectangle('Position',[randn,randn,randn,randn]);
      end

I have written a timer function

function timerfcn1(~,~,A)
    axes(A.axes1);
    rectangle('Position',[randn,randn,randn,randn]);
    axes(A.axes2);
    rectangle('Position',[randn,randn,randn,randn]);
    axes(A.axes3);
    rectangle('Position',[randn,randn,randn,randn]);
end

My problem is that during the initialization i.e when i call the constructor, the rectangles are plotted in the GUI window. However, whenever the timerfcn1 runs it creates a new figure and plots the rectangle in that.

I had used a similar thing before and it used to work then.

Was it helpful?

Solution

Most likely it's related to handle visibility (or lack of) in a part of the code that you haven't shown. To guarantee that the rectangle goes to the axis you want use,

rectangle('Position',[...],'Parent',A.axes1)

Before this you should also check that the handle still exists using

if ishandle(A.axes1)
...
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top