views:

18

answers:

1

I would like to plot discrete values in MATLAB like this:

stairs() and stem() make similar plots but can I configure one of them to look like the image above?

+2  A: 

You have to create the plot yourself.

%# create some random data
data = randn(100,1);

%# sort ascending
data = sort(data(:)); %# make column vector, just in case

%# count
nData = length(data);

%# create data to plot (open, closed circles)
yData = linspace(0,1,nData-1)'; %'# SO formatting

closedX = data(1:end-1);
closedY = yData;

openX = data(2:end);
openY = yData;

%# lines are from open to close with NaN for where there should be no line
lineX = [closedX,openX,NaN(nData-1,1)]'; %'# SO formatting
lineX = lineX(:);
lineY = [closedY,openY,NaN(nData-1,1)]'; %'# SO formatting
lineY = lineY(:);

%# plot
figure %# I like to open a new figure before every plot
hold on
plot(lineX,lineY,'r')
plot(closedX,closedY,'ro','MarkerFaceColor','r')
plot(openX,openY,'ro','MarkerFaceColor','w')
Jonas
When I run this it gives me `Error using ==> horzcat ` `CAT arguments dimensions are not consistent.` at the `lineY = [closedY,openY,NaN(nData-1,1)]';` line. I don't really know what to do with it.
sekmet64
@sekmet64: Yes, I just noticed. I fixed this, as well as several other errors. Now it runs fine.
Jonas
Wow, thanks! It looks exactly like I wanted.
sekmet64