Callback Function for update plot with multiple functions

2 ビュー (過去 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 Center および File ExchangeApp Building についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by