Read data from editing text in GUI

Hi guys, I need support about reading data from Matlab GUI.
In particular, I've written this code in my GUI but when I'm pushing on PUSHBUTTON1 it gives error Error while evaluating uicontrol Callback Undefined variable "handles" or class "handles.edit2".
The program doesn't read value for variable zeta, whilst the variable interval works.
How could I resolve this?
Thank you for your support.
function pushbutton1_Callback(hObject, eventdata, handles)
interval=str2double(get(handles.edit1,'string'));
t=0:interval:10;
initial_x = 0;
initial_dxdt = 0.1;
x0=[initial_x initial_dxdt];
[t,x]=ode45( @rhs, t, [initial_x initial_dxdt]);
plot(t,x(:,1))
function dxdt=rhs(t,x)
m=1;
k=10;
omega_n=sqrt(k/m);
zeta=str2double(get(handles.edit2,'string'));
F=sin(10*t);
dxdt_1 = x(2);
dxdt_2 = -2*zeta*omega_n*x(2) -(omega_n)^2*x(1) + sin(10*t);
dxdt=[dxdt_1; dxdt_2];

 Accepted Answer

Cris LaPierre
Cris LaPierre on 9 Aug 2020
Edited: Cris LaPierre on 9 Aug 2020
Keep in mind variable scope for functions. Look at the inputs to your function rhs
function dxdt=rhs(t,x)
The handles structure is not an input to this function, so it is not accessible inside it. This is what is causing the error in your calculation of zeta.
Try making handles an input to your function, or make the value of edit2 an input to rhs.

4 Comments

You have added handles as an input to rhs, but when you call ode45, you are not passing in the handles structure based on this error message:
Not enough input arguments.
...
Error in guide_ODE>pushbutton1_Callback (line 128)
[t,x]=ode45( @rhs, t, [initial_x initial_dxdt]);
See this post on how to add extra parameters. You should be able to do something like this.
[t,x]=ode45( @rhs, t, [initial_x initial_dxdt], handles);
I've tried this code
function pushbutton1_Callback(hObject, eventdata, handles)
h = str2double(get(handles.edit1,'string'))
[t, x] = adamsbash(@example, [0 10], [0 0.1], handles,h);
function [t, x] = adamsbash(f, tspan, x0, handles, h)
tspan=[0 10];
a=tspan(1);
b=tspan(2);
n=(b-a)/h;
t=(a+h:h:b)';
%Runge Kutta for x1
k1=feval(f, a, x0)';
k2=feval(f, a+h/2, x0+0.5.*k1.*h)';
k3=feval(f, a+h/2, x0+k2.*0.5.*h)';
k4=feval(f, a+h, x0+k3.*h)';
i=1;
x(i,:)=x0 + ((k1+2.*k2+2.*k3+k4)./6).*h;
fVal = feval(f, t(i), x0 )';
t(i+1)=t(i)+h;
x(i+1,:) = x(1,:) + h * fVal;
for i = 2 : n
fVal_prev=fVal;
fVal = feval( f,t(i), x(i,:) )';
t(i+1) = t(i) + h;
x(i+1,:) = x(i,:) + h * ( 3 * fVal - fVal_prev ) / 2;
end
x=[x0;x];
t=[a;t];
% length(t)
% size(x)
plot(t,x(:,1))
xlim([a b])
function f=example(t,x,handles)
damping = str2double(get(handles.edit2,'string'))
m=1;
k=10;
omega_n=sqrt(k/m);
F=sin(10*t);
A=[0 1; -(omega_n)^2 -2*damping*omega_n];
B=[0 F/m]';
x=[x(1) x(2)]';
f=A*x+B;
but it gives error
Error using guide_ODE>example (line 171)
Not enough input arguments.
Error in guide_ODE>adamsbash (line 142)
k1=feval(f, a, x0)';
Error in guide_ODE>pushbutton1_Callback (line 127)
[t, x] = adamsbash(@example, [0 10], [0 0.1], handles,h);
Where is the error? I've tried them all :(
You need to call a function with all its inputs. example, which you pass in as f to adamsbash, has 3 inputs: t,x, and handles.
function f=example(t,x,handles)
However, when you first use it to compute k1, you call it with only two inputs, a and x0. Hence the error message that there are not enough input arguments. You didn't include handles.
k1=feval(f, a, x0)';
Every time you call the function example you need to provide 3 inputs. When calculating k1, that means doing something like this:
k1=feval(f, a, x0,handles)';
It works! Thank you so much for the help. You have been incredibly helpful for me.

Sign in to comment.

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!