필터 지우기
필터 지우기

Help: Pass data from seperate .m file to GUI .m file?

조회 수: 3 (최근 30일)
Khanh
Khanh 2014년 10월 8일
댓글: Khanh 2014년 10월 8일
Hello everybody,
I have a .m file (named file 1) generated from .fig file and a seperate .m file to calculate (named file 2). There're two values a and b from end user through GUI. These values will be passed to file 2 to calculate a + b. The result will be assigned to res variable in file 2. When user pushes a button in GUi, it will show res variable in the static text field.
Could someone please tell me how to do it?
The code of .m file 1. I also attached files below.
function y=file2(handles)
clc
a=str2num(get(handles.a,'string'));
b=str2num(get(handles.b,'string'));
res=a+b;
%
  댓글 수: 2
Stephen23
Stephen23 2014년 10월 8일
편집: Stephen23 2014년 10월 8일
The names of the files and the function names do not match ( file1.m contains function file2 ). The text editor should have highlighted this.
Khanh
Khanh 2014년 10월 8일
This is the approach by using 3 files. I recently found out on Youtube. The file 1 can retrieve the data from GUI and calculate.

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

답변 (1개)

Stephen23
Stephen23 2014년 10월 8일
Almost there. You used get to retrieve data from the edit fields, now you can use set to assign the value res to the text field.
  댓글 수: 2
Khanh
Khanh 2014년 10월 8일
Thanks for your reply.
I also tried it. I put the
set(handles.res,'string',num2str(res))
in the file 2.
GUI showed it eventhough I didn't push the button. It wasn't what I want.
Stephen23
Stephen23 2014년 10월 8일
편집: Stephen23 2014년 10월 8일
Yes, the set command needs to happen in the pushbutton's callback function . For this you can easily define your own anonymous function which includes the set command.
The concept is this:
  • The button is pressed.
  • The button calls its callback function (which you can define).
  • Your callback function sets the string value (or does anything else that you want to do).
This is explained in more detail in the docs:

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

카테고리

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