简体   繁体   中英

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

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM