One legend from two for loops.

2 views (last 30 days)
CPdaCE
CPdaCE on 11 Aug 2015
Commented: CPdaCE on 11 Aug 2015
Hello,
I was working on a code and am running two for loops to build the plots that I need. I would, however, like to have one legend for both of the plots. I can get the 2nd legend but not the first. I'm guessing this is because the for loop pops out the last iteration? I dk I'll copy my code and provide the proper file so anyone interested can check out the code. Any help would be greatly appreciated. Also, any input on code improvements would be cool as well.
Thank you
Code:
%%Shear Beam - Analysis
clear all
close all
clc
set(0,'DefaulttextFontName','Times New Roman')
set(0,'DefaultAxesFontName', 'Times New Roman')
set(0,'DefaultAxesFontSize',12)
set(0,'DefaultTextFontSize',14)
addpath(genpath('D:/Research'))
load Combined_data_Points.txt;
%%A. Data Location
data = Combined_data_Points;
% 90" Span
time_9 = data(:,2);
mark_9 = data(:,3);
SP1 = data(:,4)*-1;
SP2 = data(:,5)*-1;
SP3 = data(:,6)*-1;
Tilt0 = data(:,7);
Tilt1 = data(:,8);
PB_9 = data(:,9);
PB_9m = [PB_9 PB_9 PB_9];
SP9 = [SP1 SP2 SP3];
% 72" Span
time_7 = data(:,11);
mark_7 = data(:,12);
SP4 = data(:,13)*-1;
SP5 = data(:,14)*-1;
SP6 = data(:,15)*-1;
Tilt70 = data(:,16);
Tilt71 = data(:,17);
PB_7 = data(:,18);
PB_7m = [PB_7 PB_7 PB_7];
SP7 = [SP4 SP5 SP6];
%%B. Load vs Deflection
x = [0 0 0];
c = [0.5 0.5 0.5];
a = 50;
shape = ['d' 'o' 's'];
for i = 1:3
SPx=SP9(:,i);
PBx = PB_9m(:,i);
scatter(SPx,PBx,a,x,'filled',shape(1,i))
grid on
axis([0 .3 0 100000])
hold on;
title('UHPC Shear Load vs Displacement','fontsize',14)
xlabel('Displacement, in')
ylabel('Load, kips')
end
hold on;
for i = 1:3
SPy = SP7(:,i);
PBy = PB_7m(:,i);
scatter(SPy,PBy,a,c,'filled',shape(1,i))
grid on
axis([0 .3 0 100000])
title('UHPC Shear Load vs Displacement','fontsize',14)
xlabel('Displacement, in')
ylabel('Load, kips')
end
Legend = cell(2,1)
Legend{1} = 'N. Quarter Pt.9 Mid Span9 S. Quarter Point9';
Lengend{2} = 'N. Quarter Pt.7 Mid Span7 S. Quarter Point7';
legend(Lengend);
hold off;

Answers (1)

Star Strider
Star Strider on 11 Aug 2015
I can’t run your code because I don’t have your data file. However to add a legend to an axis object, return a handle to the axis object and then specify it in the legend call:
x1 = rand(50, 1);
y1 = rand(50, 1);
x2 = rand(50, 1);
y2 = rand(50, 1);
figure(1)
sh1 = scatter(x1, y1);
figure(2)
sh2 = scatter(x2, y2);
Legend{1} = 'N. Quarter Pt.9 Mid Span9 S. Quarter Point9';
Legend{2} = 'N. Quarter Pt.7 Mid Span7 S. Quarter Point7';
legend(sh1, Legend);
legend(sh2, Legend);
  2 Comments
CPdaCE
CPdaCE on 11 Aug 2015
The data file is attached. It's called Combined_data_Points.txt. I ran your code and saw that it works. But my problem is that I have a changing marker value for each. So three different marker shapes and two different colors on the same graph. Each marker and color need their own legend entry. Thank you for responding, appreciated.
CPdaCE
CPdaCE on 11 Aug 2015
When I scatter plot both data plots within the same for loop this is something that I get. I need a smarter legend or code hahah. Thanks

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!