필터 지우기
필터 지우기

Read data from editing text in GUI

조회 수: 4 (최근 30일)
Antonio Tricarico
Antonio Tricarico 2020년 8월 8일
댓글: Antonio Tricarico 2020년 8월 12일
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];

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 8월 9일
편집: Cris LaPierre 2020년 8월 9일
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
Cris LaPierre
Cris LaPierre 2020년 8월 11일
편집: Cris LaPierre 2020년 8월 11일
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)';
Antonio Tricarico
Antonio Tricarico 2020년 8월 12일
It works! Thank you so much for the help. You have been incredibly helpful for me.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by