0

I would like to plot a series of rectangular pulse to a vector data input. The plot profile would create a rising edge of the pulse for a positive number and create the falling edge for the negative of that number. The plot should separate color for each represented number.

For example, if vector input X is [1 -1 2 -2 3 4 1 -4 -1 -3] Amplitude of data ‘1’ is 5, Amplitude of data ‘2’ is 4, Amplitude of data ‘3’ is 3 and Amplitude of data ‘4’ is 2

So, the input X got index from t(1) to t(10). The output of plot or chart should look like the inserted imageenter image description here

How would I can write a MATLAB code for this work?

Thank you. B.Bundit

user3464608
  • 33
  • 1
  • 6

1 Answers1

0

The plot is very small but I think you want something like bar in matlab. Documentation is here. If the vector that you have is changes then you can create a vector of values for plotting using cumsum.

X=[1 -1 2 -2 3 4 1 -4 -1 -3];
Xplot=cumsum([5,X(2:end)]); % //so 5 will be your initial value

Since the plot you give above has different widths of bars, you would also need a vector of the center of each data point and the width of each data point.

means= [1 2 3 4 5 6 7 8 9 10];
widths=[1 1 1 1 1 1 1 2 1 1];

For different color bar plots you can do:

colors=['r','g','k','b','c','m','y','r','g','k'];
for i=1:length(X)
    h=bar(means(i),Xplot(i),widths(i));
    if i==1, hold on; end
    set(h,'FaceColor',colors(i));
end
% //This will label each bar, or you could define your axes before the loop
set(gca,'XTickLabel',means);

You can explore other properties to set here. The class barseries does not have a FaceAlpha property so I'm not sure if you can make them transparent. You could however set the FaceColor to none and have the EdgeColor be set to your color of choice. Note that edge color is specified by an RGB triplet and not a letter code.

You may also need to resort your data by width, so that the widest bars are plotted last and are thus on top. This would look like this:

[widths_sorted,sort_idx]=sort(widths,'ascending');
Xplot_sorted=Xplot(sorted_idx);
means_sorted=means(sorted_idx);
Raab70
  • 721
  • 3
  • 11
  • Hi Raab70, Thanks for your comment. But your code doesn't give the plot looks like the plot image I provided. Indeed, the code should include the pulse/bar amplitude of each indication number and should do separate color for each number too (4 colors for 4 numbers in the example). Thanks. – user3464608 May 01 '14 at 05:41
  • After plotting by your codes, it is still not similar with what it should be (as the image). The amplitude and width of each bar (number) still be incorrect. Thanks for helps. – user3464608 May 03 '14 at 14:55
  • How are they incorrect? How did you set the widths? I just picked some random numbers as an example. The amplitudes should be correct as you described above as long as you have the starting point correct. As I mentioned it will never look exactly like the image you provided since matlab cannot make the bars transparent. – Raab70 May 04 '14 at 13:42