Search code examples
matlabplotmatlab-figure

Matlab bar plot in specific time points of each measured values


I want to plot vertical lines corresponding to values obtained at specific time points.

Example:

a = [0    5  7    9 ] at 0 seconds, 
b = [0.5  6  6.5  11] at 2 seconds,
c = [0    4  2    10] at 4 seconds

Each time point will be a vertical line between the maximum and minimum of the vectors. I also need to mark the start and end points of a, b and c, for instance a should have a circle (or star etc.) at 0 and 9.

Here is an example output:

Example Image


Solution

  • You can use line with end markers.

    % Your data
    a = [0    5   7    9 ];
    b = [0.5  6   6.5  11];
    c = [0    4   2    10];
    % Combine to get min/max values
    data = [a; b; c].';
    mins = min(data);
    maxs = max(data);
    % Plot using line, nice flexible method which plots vertical lines at points 2:2:n 
    line(repmat(2*(0:numel(mins)-1), 2, 1), [mins; maxs], 'color', 'k', 'marker', 'o')
    

    Output:

    plot


    If you want different markers on each end, or different colours, please see this answer which gives more detailed examples.