Problem with graphing different plots on the same graph within a ‘for’ loop.
2 views (last 30 days)
Show older comments
Hi,
I just have a problem with graphing different plots on the same graph within a ‘for’ loop. I hope someone can be point me in the right direction.
I have a 2-D array, with discrete chunks of data in and amongst zeros. My data is the following:
A=
0 0
0 0
0 0
3 9
4 10
5 11
6 12
0 0
0 0
0 0
0 0
7 9.7
8 9.8
9 9.9
0 0
0 0
A chunk of data is defined as contiguous set of data, without interruptions of a [0 0] row. So in this example, the 1st chunk of data would be
3 9
4 10
5 11
6 12
And 2nd chunk is
7 9.7
8 9.8
9 9.9
The first column is x and second column is y. I would like to plot y as a function of x (x is horizontal axis, y is vertical axis) I want to plot these data sets on the same graph as a scatter graph, and put a line of best fit through the points, whenever I come across a chunk of data. In this case, I will have 2 sets of points and 2 lines of best fit (because I have 2 chunks of data). I would also like to calculate the R-squared value
The code that I have so far is shown below:
fh1 = figure;
hold all;
ah1 = gca;
% plot graphs:
for d = 1:max_number_zeros+num_rows
if sequence_holder(d,1)==0
continue;
end
c = d;
while sequence_holder(c,1)~=0
plot(ah1,sequence_holder(c,1),sequence_holder(c,num_cols),'*');
%lsline;
c =c+1;
continue;
end
end
Sequence holder is the array with the data in it. I can only plot the first set of data, with no line of best fit. I tried lsline, but that didn't work.
Can anyone tell me how to
-plot both sets of graphs
-how to draw a line of best fit a get the regression coefficient?
0 Comments
Accepted Answer
dpb
on 4 May 2015
Edited: dpb
on 5 May 2015
For each group over the ranges,
scatter(x,y) % where x,y are over the ranges, of course
if loopIndex==1, hold on, end % add to the plot
b=polyfit(x,y,1); % also as above...
yhat=polyval(b,[x(1) x(end)]); % evaluate the fit at end points
plot([x(1) x(end)],yhat); % plot the fitted line
Repeat the above for each set of indices in the groups...there are multiple other ways to attack such a problem; one is that can replace 0>>NaN and plot all the points at once; the NaN values will just be ignored. All will be one color for the data points that way which may (or may not) be desired effect.
2 Comments
dpb
on 5 May 2015
You include it within the loop wherein you've determined the sections and number thereof; each pass uses the indices of that subsection.
The if clause simply sets hold on after the first plot--once it's 'on' there's no point in doing it over and over. That's the "trick" to add additional information on the same axes object; the first plot is created with the defaul condition of hold off; then 'on' retains that data and some other state variables for the express purpose of adding to that plot. See
doc hold % for details
More Answers (0)
See Also
Categories
Find more on Linear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!