Callback Function for update plot with multiple functions

2 views (last 30 days)
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 Comments
Maxsam Donta
Maxsam Donta on 24 Sep 2017
Edited: Maxsam Donta on 24 Sep 2017
The slider should change the value of k and should adjust the plot accordingly

Sign in to comment.

Answers (2)

Rik
Rik on 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').
  1 Comment
Maxsam Donta
Maxsam Donta on 24 Sep 2017
Thank you for your response. There are no longer errors when I do this, but the graph doesn't change when I move the slider.

Sign in to comment.


Jan
Jan on 24 Sep 2017
Edited: Jan on 24 Sep 2017
[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.
  1 Comment
Maxsam Donta
Maxsam Donta on 24 Sep 2017
Thank you for your response. I am very new to Matlab and programming in general, so I unfortunately I'm lost with these references as for what they mean and what to do.

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!