How to create a Legend in UIextras?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I created some small calculator/plotter, but I would like to add a legend to the plot, which does not work:
Here is a working example, just look for the commented section:
function travel_time_slider()
close(findobj('tag', 'travel_time_slider'))
f = figure('Tag', 'travel_time_slider', 'Position', [ 680 307 560 791]);
handles.Figure = f ;
vbox = uiextras.VBox( 'Parent', f, 'Padding', 15);
handles.vbox = vbox;
handles.axes = axes('Parent', vbox);
[x, ladezeit, fahrzeit, gesamt, n, l, kW, kWh] = init_plot() ;
handles.ladezeit = plot(x, ladezeit, 'DisplayName', sprintf('Total charging time (%4.2f min per charge)', l*60), 'linewidth', 2); grid on, hold on
handles.fahrzeit = plot(x, fahrzeit, 'DisplayName', sprintf('Driving time for %g km', n) , 'linewidth', 2) ;
handles.gesamt = plot(x, gesamt, 'DisplayName', 'Sum' , 'linewidth', 2) ;
title(sprintf('Travel time for %d km with %d kW charger and %d kWh charge', n, kW, kWh))
xlabel('Velocity [km/h]')
ylabel('Travel time [h]')
ylim([0 20])
%
%
% THIS PARD DOES NOT WORK
%
legend(handles.axes, 'location', 'north')
%
% % COMMENT IT OUT AND IT'LL WORK WITHOUT A LEGEND
%
%
handles.slider_km = uicontrol('Parent', vbox, 'Style', 'slider',...
'Min',100,'Max',2000,'Value',1000,...
'Callback', @draw);
handles.text_km = uicontrol('Parent', vbox, 'Style', 'text',...
'FontSize', 14, 'FontWeight', 'bold', ...
'String','Driving distance: 1000 km');
handles.slider_kWh = uicontrol('Parent', vbox, 'Style', 'slider',...
'Min',20,'Max',200,'Value',50,...
'Callback', @draw);
handles.text_kWh = uicontrol('Parent', vbox, 'Style', 'text',...
'FontSize', 14, 'FontWeight', 'bold', ...
'String','Electric battery charge: 50 kWh');
handles.slider_kW = uicontrol('Parent', vbox, 'Style', 'slider',...
'Min',20,'Max',400,'Value',50,...
'Callback', @draw);
handles.text_kW = uicontrol('Parent', vbox, 'Style', 'text',...
'FontSize', 14, 'FontWeight', 'bold', ...
'String','charger power: 50 kW');
set(vbox, 'Sizes', [-1 20 25 20 25 20 25]) ;
guidata(f, handles)
end
function [x, ladezeit, fahrzeit, gesamt, n, l, kW, kWh] = init_plot(n, kWh, kW)
p = polyfit([80 120 200], [15 20 65], 2) ;
x = linspace(100,200);
if ~exist('n', 'var')
n = 1000 ; % Kilometer
end
if ~exist('kWh', 'var')
kWh = 50 ; % Akkukapazität
end
if ~exist('kW', 'var')
kW = 120 ; % Ladeleistung
end
l = kWh / kW ; % Ladezeit
ladezeit = (ceil(n ./ (kWh ./ polyval(p, x) * 100))-1) * l ;
fahrzeit = n ./ x ;
gesamt = fahrzeit + ladezeit ;
end
function draw(hObject, ~)
handles = guidata(hObject) ;
n = handles.slider_km.Value ;
kWh = handles.slider_kWh.Value ;
kW = handles.slider_kW.Value ;
[x, ladezeit, fahrzeit, gesamt, n] = init_plot(n, kWh, kW) ;
handles.gesamt.XData = x ; handles.gesamt.YData = gesamt;
handles.ladezeit.XData = x ; handles.ladezeit.YData = ladezeit;
handles.fahrzeit.XData = x ; handles.fahrzeit.YData = fahrzeit;
title(sprintf('Travel time for %d km with %3.0f kW charger and %3.0f kWh charge', n, kW, kWh))
handles.text_kWh.String = sprintf('Electric battery charge: %5.2f kWh', kWh) ;
handles.text_kW.String = sprintf('charger power: %3.0f kW', kW) ;
handles.text_km.String = sprintf('Driving distance: %4.0f km', n) ;
end
How can I make it work with a legend?
0 comentarios
Respuestas (0)
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!