Legend not working in 2024 as it did in Version 6.1.
    10 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
It seems that the legend is giving the correct entry for the Weight but giving an entry for every data point for Median and Mean.
0 Commenti
Risposte (2)
  Star Strider
      
      
 il 30 Apr 2025
        Ii cannot get the code to run here.  
Change the legend call to: 
legend([hw,hmedw(1),hmeanw(1)], 'Weight','Median','Mean', 'Location','northeastoutside')
and it should do what you want.  
type('Weight_6p1')
clear all
format long g
Wght = [243.4;248.2;247.4;247.4;247.8;...
        246.0;250.8;248.2;248.4;246.6;...
        248.8;246.2;246.4;245.4;246.2;...
        246.6;];                    % Weight data
warning off
Days = (1:size(Wght)); % Number of data points
medW = median(Wght);   % Median - a median of a population is any value such that 
                       % at most half of the population is less than the proposed median 
                       % and at most half is greater than the proposed median
meanW = mean(Wght);    % Mean - Average   
hold on % Hold graph to plot other lines
grid on % Grid
hw = plot(Days,Wght,'bd-');                                  % Handle for the plot of the Weight
hmedw = plot(Days,medW,'kx-');                               % Handle for the plot of the Median
hmeanw = plot(Days,meanW,'ro-');                             % Handle for the plot of the Mean
% legend([hw,hmedw(1),hmeanw(1)], 'Weight','Median','Mean',-1) % Legend using the Handle and -1 for outside the box upper right
legend([hw,hmedw(1),hmeanw(1)], 'Weight','Median','Mean', 'Location','northeastoutside') % Legend using the Handle and -1 for outside the box upper right
axis([1 91 170 260])                     % x,y values
xlabel('Readings')
yl = ylabel('Weight (lbs)');             % Handle for ylabel
set(yl,'Position',[-8 239.912 17.3205]); % set position for ylabel
t = title('Weight');
set(t, 'FontSize', 18);                  % Enlarge title
Array = ones(size(Days));                % Create an array of ones for the number of data points
linemed = Array*medW;                    % Create Median line
plot(Days,linemed,'k','Linewidth',2);    % Plot line
text(-3.8, medW, sprintf('%0.1f',medW)); % Place text with 1 decimal point
box on %Surround entire axis with line
line180 = Array*180;
plot(Days,line180,'k','Linewidth',3);
gtext('Optimal Weight');
hsc = gtext({'The Mean Value of the Weight is:';sprintf('%0.2f',meanW)}); % Prints out a 1 x 2 using semicolon between elements 
                                                                          %but print out of elements is too far apart
gtext('lbs');
hsd = gtext({'The Median Value of the Weight is:';sprintf('%0.2f',medW)});
gtext('lbs');
% run('Weight_6p1')
% fprintf(['\n' repmat('=',1,72) '\n'])
% run('Weight_2024')
.
2 Commenti
  Voss
      
      
 il 30 Apr 2025
        medW and meanW are scalars, so plotting them against the vector Days creates multiple lines (one per data point, as you observe - but the problem is due to plot, not legend). To fix this, when plotting medW and meanW, plot a vector the same size as Days, exactly as you do with line180. Below I use the same approach for plotting medW and meanW that you were already using for line180, which is to multiply by Array.
clear
format long g
Wght = [243.4;248.2;247.4;247.4;247.8;...
        246.0;250.8;248.2;248.4;246.6;...
        248.8;246.2;246.4;245.4;246.2;...
        246.6;243.2;244.2;246.2;245.8;...
        246.6;241.5;244.2;244.2;244.8;...
        243.4;242.6;243.4;242.2;245.6;...
        243.4;244.0;245.2;245.2;248.4;...
        247.4;244.8;244.8;246.0;247.0;...
        248.6;247.8;245.8;245.8;246.4;...
        246.6;243.6;244.8;246.0;245.4;...
        244.2;245.0;245.6;246.6;245.2;...
        243.8;242.8;242.0;242.8;240.6;...
        241.8;242.2;242.2;243.8;244.2;...
        241.8;245.0;243.8;244.8;246.2;...
        247.2;246.6;249.4;247.0;245.2;...
        245.4;247.2;247.6;247.6;244.4;...
        241.6;239.4;239.8;241.2;239.8;...
        244.0;244.0;242.8;242.0;];                    % Weight data
warning off
Days = 1:size(Wght); % Number of data points
medW = median(Wght);   % Median - Half above Half below 
meanW = mean(Wght);    % Mean - Average   
hold on % Hold graph to plot other lines
grid on % Grid
whos Days Wght medW meanW
Array = ones(size(Days)); 
plot(Days,Wght,'bd-','DisplayName','Weight'); % plot of the Weight
plot(Days,medW*Array,'kx-','DisplayName','Median'); % plot of the Median
plot(Days,meanW*Array,'ro-','DisplayName','Mean');  % plot of the Mean
axis([1 91 170 260])                     % x,y values
xlabel('Readings')
yl = ylabel('Weight (lbs)');             % Handle for ylabel
set(yl,'Position',[-8 239.912 17.3205]); % set position for ylabel
t = title('Weight');
set(t, 'FontSize', 18);                  % Enlarge title
box on %Surround entire axis with line
line180 = Array*180;
whos Days line180
plot(Days,line180,'k','Linewidth',3,'DisplayName','Optimal Weight');
legend
Vedere anche
Categorie
				Scopri di più su Annotations in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



