Abdul Rehman
Abdul Rehman

Reputation: 2346

Plotting 3 vectors in Matlab GUI axes handle

I am trying to plot 3 vectors onto matlab GUI in a serial object's callback. I want to plot this on axes handle but the problem is it only plot last vector;

    plot(handles.axes1,sensor1,'r');
    plot(handles.axes1,sensor2,'b');
    plot(handles.axes1,sensor3,'g');

I searched on internet and find that this issue can be solved with hold on and hold of feature so I tried this

    plot(handles.axes1,sensor1,'r');
    hold on ;
    plot(handles.axes1,sensor2,'b');
    plot(handles.axes1,sensor3,'g');
    hold off;

but in this case a new figure is opened(dont know why) and again only the last plot is drawn.

I am stucked. If any one have idea of what would be the issue? Thanks

Upvotes: 0

Views: 944

Answers (2)

Steve Osborne
Steve Osborne

Reputation: 680

I'm not sure why your first try using "hold" didn't work. Seems like it should have.

But in any case, you can get the desired behavior in a single command:

plot(handles.axes1,length(sensor1),sensor1,'r',...
                   length(sensor2),sensor2,'b',...
                   length(sensor3),sensor3,'g');

This specifies both an X = length(sensor_) and a Y = sensor_ to the plot command. When you only give plot a Y input, it assumes an X of length(Y). But you can't combine multiple traces in a single plot command by giving only the Y input for each, because it will try to treat the inputs as X,Y pairs.

Upvotes: 1

RTL
RTL

Reputation: 3587

As the vectors are the same length we can simply combine them as the columns of a matrix and then plot the matrix

plot(handles.axes1,[sensor1',sensor2',sensor3'])

However these will have the default colour order. Without specifying x values setting colors within the plot command is tricky. However (luckily) the default order starts:

blue,green,red...

so swapping the column order will plot the lines with the colours requested

plot(handles.axes1,[sensor2',sensor3',sensor1'])

(this assumes the vectors are rows, if they are columns don't transpose them)

Upvotes: 0

Related Questions