Callback function not able to update System
Mostrar comentarios más antiguos
I am implementing a slider for a certain variable in a spring-damper system and trying to update the plot according to the value in the slider using the callback fn. but somehow not able to update it.
clc
clear
close all
m = 8 %mass
k = 1 %stiffness
c = 0.5 %damping
time = 0 : 0.1 : 10;
t = sym ('t')
x_0= 0.01; % Initial Condition displacement
x_dot_0= 0; % Initial Condition velocity
syms x(t)
Dx = diff(x,1);
D2x = diff(x,2);
x = dsolve( m*D2x + c*Dx + k*x == 0 ,x(0)==x_0 ,Dx(0) == x_dot_0, 't'); % Initial conditions units are [m] and [m/s] respectively
x_fun = matlabFunction(x);
sys = x_fun(time);
f = figure;
ax = axes('Parent',f,'position',[0.13 0.42 0.77 0.54]);
h1 = plot(time,sys);
bgcolor = f.Color;
b = uicontrol('Parent',f,'Style','slider','Position',...
[81,130,419,23],'value',c, 'min',0, 'max',5);
bl3 = uicontrol('Parent',f,'Style','text','Position',[240,115,100,23],...
'String','Damping Ratio','BackgroundColor',bgcolor);
bl1 = uicontrol('Parent',f,'Style','text','Position',[50,130,23,23],...
'String','0','BackgroundColor',bgcolor);
bl2 = uicontrol('Parent',f,'Style','text','Position',[500,130,23,23],...
'String','5','BackgroundColor',bgcolor);
b.Callback = @(es,ed) updateSystem(h1,dsolve(m*D2x + c*Dx + (es.Value) *x == 0 ,x(0)==x_0 ,Dx(0) == x_dot_0, 't'));
Respuestas (1)
Rik
el 12 de Nov. de 2020
I prefer to use actual functions for my GUIs, see if this helps. Usually this also really helps with debugging.
m = 8; %mass
k = 1; %stiffness
c = 0.5; %damping
time = 0 : 0.1 : 10;
t = sym ('t');
x_0= 0.01; % Initial Condition displacement
x_dot_0= 0; % Initial Condition velocity
syms x(t)
Dx = diff(x,1);
D2x = diff(x,2);
x = dsolve( m*D2x + c*Dx + k*x == 0 ,x(0)==x_0 ,Dx(0) == x_dot_0, 't'); % Initial conditions units are [m] and [m/s] respectively
x_fun = matlabFunction(x);
sys = x_fun(time);
f = figure(1);clf(f)
ax = axes('Parent',f,'position',[0.13 0.42 0.77 0.54]);
h1 = plot(time,sys);
bgcolor = f.Color;
b = uicontrol('Parent',f,'Style','slider','Position',...
[81,130,419,23],'value',c, 'min',0, 'max',5,...
'Callback',@callback_wrapper);
bl3 = uicontrol('Parent',f,'Style','text','Position',[240,115,100,23],...
'String','Damping Ratio','BackgroundColor',bgcolor);
bl1 = uicontrol('Parent',f,'Style','text','Position',[50,130,23,23],...
'String','0','BackgroundColor',bgcolor);
bl2 = uicontrol('Parent',f,'Style','text','Position',[500,130,23,23],...
'String','5','BackgroundColor',bgcolor);
handles=struct;
[handles.f,handles.h1]=deal(f,h1);
vars=struct;
[vars.m,vars.c,vars.x_0,vars.x_dot_0,vars.x,vars.Dx,vars.D2x]=deal(...
m,c,x_0,x_dot_0,x,Dx,D2x);
handles.vars=vars;
guidata(handles.f,handles)
function callback_wrapper(es,ed)
handles=guidata(es);vars=handles.vars;
[m,c,x_0,x_dot_0]=deal(...
vars.m,vars.c,vars.x_0,vars.x_dot_0);
k=es.Value;
syms x(t)
Dx = diff(x,1);
D2x = diff(x,2);
sys=dsolve(m*D2x + c*Dx + k*x == 0 ,x(0)==x_0 ,Dx(0) == x_dot_0, 't');
updateSystem(handles.h1,sys)
end
2 comentarios
Abdelrahman Darwish
el 12 de Nov. de 2020
Editada: Abdelrahman Darwish
el 12 de Nov. de 2020
Rik
el 12 de Nov. de 2020
Does that function work when using it in isolation?
Categorías
Más información sobre App Building en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!