I'm writing my Rsquared values onto a scatter plot but they don't appear on the correct suplot!

1 view (last 30 days)
I've managed to print my Rsquared values onto scatter subplots but for some reason it's shifted each value onto the wrong graph by one. I have 8 graphs and the last graph for some reason doesn't get an Rsquared value printed on it!
My code for this part is:
for j = 2:9 %change 9 to 8 when removing one KPI
y = T{:,j};
R = corrcoef(x,y);
R_squared = R(2)^2;
text(5, 5, ['R^2 =' num2str(R_squared)]);
ax = subplot(2,4,(j-1));
scatter(ax,x,y,'filled');
lsline(ax);
ylim([0,max(y+10)]);
xlim([0,8]);
xlabel('League Score');
ylabel(T.Properties.VariableNames{(j)});
end;
So obviously the text part is what prints the rsquared values to my graphs.
Thank you!!

Accepted Answer

Geoff Hayes
Geoff Hayes on 17 Oct 2015
Olivia - the problem may be because you are not specifying which axes to write the text to. According to the documentation, the text is written to the current axes and so you will have to specify which axes is the current one before you invoke text. (It may be that on subsequent iterations of your loop, the current axes is always the last one updated.) Try doing the following in your loop
ax = subplot(2,4,(j-1)); % get the axes
axes(ax); % set ax as the current axes
text(5, 5, ['R^2 =' num2str(R_squared)]);
scatter(ax,x,y,'filled');
% etc.

More Answers (0)

Categories

Find more on Graphics Object Properties 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!