Callback Function for update plot with multiple functions
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, I need the slider bar to adjust all three of the functions on the plot, but I don't know how to make a callback to update it. Plot picture is attached and c

ode is here:
% 3.131
k = 180; %W/m K
L = 10e-3; %m
t = 1e-3; %m
Tb = 100 + 273; %K
Tinf = 25 + 273; %K
h = linspace(10,1000,20);
m = sqrt(2 .* h ./ (k * t));
M = sqrt(2 .* h .* t .* k) .* (Tb-Tinf);
qfa = M .* (sinh(m .* L)+(h ./ (m .* k)) .* cosh(m .* L)) ./ (cosh(m .*L) + (h ./ (m .*k)).*sinh(m.*L));
qfb = M .* tanh(m.*L);
qfd = M;
f = figure;
plot(h,qfa,'k','DisplayName','qfa'); hold on;
plot(h,qfb,'b','DisplayName','qfb');
plot(h,qfd,'r','DisplayName','qfd');
xlabel('Convection coefficient, h(W/m^2 * K)'); ylabel('Heat rate,qf(W/m)'); title('Heat rate vs h for different boundary conditions');
legend('show')
b = uicontrol('Parent',f,'Style','slider','Position',[81,54,419,50],...
'value',k,'min',15,'max',180);
bgcolor = f.Color;
bl1 = uicontrol('Parent',f,'Style','text','Position',[50,54,23,50],...
'String','15','BackgroundColor',bgcolor);
bl2 = uicontrol('Parent',f,'Style','text','Position',[500,54,23,50],...
'String','180','BackgroundColor',bgcolor);
bl3 = uicontrol('Parent',f,'Style','text','Position',[240,50,100,40],...
'String','Conduction Coefficient, k(W/m K)','backgroundColor',bgcolor);
2 Kommentare
Antworten (2)
Rik
am 24 Sep. 2017
If that k should contain the value of the slider, make sure the slider exist before the first plot and the use k=get(b,'Value').
Jan
am 24 Sep. 2017
Bearbeitet: Jan
am 24 Sep. 2017
See https://www.mathworks.com/matlabcentral/answers/292993-continuous-slider-callback-not-updated-handles-in-callback-function and https://www.mathworks.com/matlabcentral/answers/264979-continuous-slider-callback-how-to-get-value-from-addlistener for defining a callback, which is triggered during moving the slider, not only when releasing it.
[EDITED] Try this:
function YourGUI
k = 180; %W/m K
FigH = figure;
handles.gfaPlot = plot(h,qfa,'k','DisplayName','qfa');
hold on;
handles.gfbPlot = plot(h,qfb,'b','DisplayName','qfb');
handles.gfdPlot = plot(h,qfd,'r','DisplayName','qfd');
xlabel('Convection coefficient, h(W/m^2 * K)');
ylabel('Heat rate,qf(W/m)');
title('Heat rate vs h for different boundary conditions');
legend('show')
handles.Slider = uicontrol('Parent',f,'Style','slider', ...
'Position',[81,54,419,50],...
'value',k,'min',15,'max',180, ...
'Callback', {@yourSliderCallback, handles});
bgcolor = f.Color;
uicontrol('Parent',f,'Style','text','Position',[50,54,23,50],...
'String','15','BackgroundColor',bgcolor);
uicontrol('Parent',f,'Style','text','Position',[500,54,23,50],...
'String','180','BackgroundColor',bgcolor);
uicontrol('Parent',f,'Style','text','Position',[240,50,100,40],...
'String','Conduction Coefficient, k(W/m K)', ...
'backgroundColor',bgcolor);
end
function [h, gfa, gfb, gfd] = YourCalculation(k)
L = 10e-3; %m
t = 1e-3; %m
Tb = 100 + 273; %K
Tinf = 25 + 273; %K
h = linspace(10,1000,20);
m = sqrt(2 .* h ./ (k * t));
M = sqrt(2 .* h .* t .* k) .* (Tb-Tinf);
qfa = M .* (sinh(m .* L)+(h ./ (m .* k)) .* cosh(m .* L)) ./ ...
(cosh(m .*L) + (h ./ (m .*k)).*sinh(m.*L));
qfb = M .* tanh(m.*L);
qfd = M;
end
function yourSliderCallback(SliderH, EventData, handles)
k = get(SliderH, 'Value');
[h, gfa, gfb, gfd] = YourCalculation(k);
set(handles.gfaPlot, 'YData', gfa);
set(handles.gfbPlot, 'YData', gfb);
set(handles.gfdPlot, 'YData', gfd);
end
Now the callback of the slider calculates the new values and updates the Y-positions of the lines. The callback is triggered, when the mouse is released. Using the "continuous slider callback" as explained at first in my message would change the value wile the slider is moved already.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!