byetisener
byetisener

Reputation: 310

How to create a marker on a existing plot on axes1 in Matlab GUIDE?

I have been trying to add a marker to my plot but I have failed so far. This is the piece of code I am working on:

v = evalin('base','a matrix in my workspace which is 1000 by 1');
  plot(v, 'Parent', handles.axes2);
  for frames = 2:handles.frameCount-1
      axes(handles.axes1);
      imshow(handles.videoStruct(frames).cdata);
      set(handles.text3, 'String', num2str(frames));
      drawnow;
      pause(1/handles.videoObject.FrameRate);
      axes(handles.axes2);
      hold on;
      plot(frames, v(frames), '.r');
  end

I have two axes in my gui. axes1 is used to display a video and has no problems. "v" is plotted in axes 2 before the video display and I would like to add a marker to it which will "move" according to which frame the for loop is at.

The last plot(frames, v(frames)... line works but plots points on axes2.

I have 2 options:

  1. Somehow delete the last marker plot when the loop moves to the next iteration. Actually I don't want to prefer this because I will implement a slider to control that marker in the future.

  2. Any easier way to create a marker on a plot without plotting it over again :)

Thank you very much from now on...

Upvotes: 0

Views: 222

Answers (1)

Aero Engy
Aero Engy

Reputation: 3608

Try this. Basically do not call plot inside the loop. It comes with a bunch of overhead which is slow. Call it everything outside the loop. Then inside use the handles to update the marker x & ydata, the images CData, and the text.

v = evalin('base','a matrix in my workspace which is 1000 by 1');
plot(handles.axes2. v);
hold(handles.axes2,'on');
%USE these handles in the loop
markH = plot(handles.axes2, 1, v(1), '.r'); %Initial plot and get handle toLine.
imH = imshow(handles.videoStruct(1).cdata, 'Parent', handles.axes1);

for frames = 2:handles.frameCount-1
    % Do no replot just set x, y, & cdata, etc.
    set(markH,'XData',frames,'YData',v(vframes))
    set(imH, 'CData',handles.videoStruct(frames).cdata);
    set(handles.text3, 'String', num2str(frames));
    drawnow;
    pause(1/handles.videoObject.FrameRate);
 end

Note: I assumed the first frame & was index 1 even though your loop started at 2. However, if that is not correct then you can easily swap out the 1s for 2s in the calls outside of the loop.

Upvotes: 1

Related Questions