Callback Function for update plot with multiple functions

조회 수: 10 (최근 30일)
Maxsam Donta
Maxsam Donta 2017년 9월 23일
편집: Jan 2017년 9월 24일
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
Walter Roberson
Walter Roberson 2017년 9월 23일
You have not defined what the slider should do .
Maxsam Donta
Maxsam Donta 2017년 9월 24일
편집: Maxsam Donta 2017년 9월 24일
The slider should change the value of k and should adjust the plot accordingly

댓글을 달려면 로그인하십시오.

답변 (2개)

Rik
Rik 2017년 9월 24일
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
Maxsam Donta
Maxsam Donta 2017년 9월 24일
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.

댓글을 달려면 로그인하십시오.


Jan
Jan 2017년 9월 24일
편집: Jan 2017년 9월 24일
[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
Maxsam Donta
Maxsam Donta 2017년 9월 24일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by