0
votes

Is there an easy command to have a plot like the blue line in the picture (excel)? Matlab defaults to produce something like the line in red. The only way I know to do this is to issue a plot command for each segment of the line:

for i=2:n-1
    plot([data(i-1,1) data(i,1)],[data(i-1,2) data(i,2)],'-b'); hold on;
end

enter image description here

1
What's the command that generated the red line and which version of Matlab are you using? - Haochen Wu
I'm using R2015, and I generate my plots using the plot(x,y); command. Is there a setting that would make my default plot the red one? I'll check again when at work, but i'm getting a plot that looks more like the red line when I plot in matlab. - andy mcevoy
Well I can't reproduce the problems I was having... guess the weekend fixed things. :) Thanks. - andy mcevoy

1 Answers

2
votes

You can just plot the entire array and let plot automatically draw straight line segments between each of the points. This is the default behaviour when plotting things in MATLAB. MATLAB plotting smooth lines is not the default behaviour when the plot is produced, so I'm not sure where you're getting that information.

You would need to perform some sort of spline interpolation to get the red line, but you desire the blue curve and so plotting the entire array in a single plot command should suffice.

It's as simple as:

plot(data(:,1), data(:,2), '-b');

Just to be sure that we're on the same page, I'm going to reproduce your data then use the above command to plot the data so you can see for yourself that the behaviour you desire is achieved:

data = [0 0; 1 1; 2 4; 3 6; 4 4]; %// Your data reconstructed
plot(data(:,1), data(:,2), '-b'); %// Main plotting code

%// Some extras
xlim([0 4.5]);
ylim([0 7]);
grid;

I've added in some extra code to get the plot to look like your example. I've made the x-axis limits go up to 4.5 and the y-axis limits go up to 7. I've also placed a grid in the plot.

We get:

enter image description here