Callback function not able to update System

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'));

Antworten (1)

Rik
Rik am 12 Nov. 2020

0 Stimmen

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 Kommentare

Abdelrahman Darwish
Abdelrahman Darwish am 12 Nov. 2020
Bearbeitet: Abdelrahman Darwish am 12 Nov. 2020
I tried your code but nothing changed, I keep trying to move the slider but the plot is not affected.
The following error is generated:
Undefined function or variable 'updateSystem'.
Error in LiveEditorEvaluationHelperE975945502>callback_wrapper (line 43)
updateSystem(handles.h1,sys)
Error while evaluating UIControl Callback.
>>
Rik
Rik am 12 Nov. 2020
Does that function work when using it in isolation?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu App Building finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2018a

Gefragt:

am 12 Nov. 2020

Kommentiert:

Rik
am 12 Nov. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by