MATLAB GUIDE and compiler

조회 수: 5 (최근 30일)
Jay
Jay 2013년 7월 31일
댓글: Mark Finn 2020년 12월 9일
I have written a very simple program using MATLAB GUIDE. I display 2 pushbuttons. When the user clicks the first pushbutton, the text "hello" appears on the console. When the user clicks the second pushbutton, the text "goodbye" appears on the console. I chose to separate the two functions pushbutton1_Callback and pushbutton2_Callback into separate .m files within the same directory as the main program. Everything works fine. Next, I compiled the program using the MATLAB compiler. When I tried to execute the compiled program, I get the error message, 'undefined function pushbutton1_Callback'. I can fix this error by taking pushbutton1_Callback and pushbutton2_Callback code and placing it back into the main program (all code in the same file instead of in separate files). What's going on? Is it a mistake to put gui functions into separate files? I'm also puzzled because if the compiler doesn't allow this, why wouldn't the error be flagged during compilation? With longer programs (thousands of lines), the program will compile but give me erroneous results (e.g. when I click on one pushbutton it will execute the code which appears under another pushbutton)
For the simple case, I've pasted in pushbutton1_Callback code below. Main program code is created entirely by GUIDE.
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
disp(sprintf('hello_2'));

채택된 답변

Matt J
Matt J 2013년 7월 31일
편집: Matt J 2013년 7월 31일
Insert this somewhere in your main file (e.g., the first line in the opening function)
%#function pushbutton1_Callback pushbutton2_Callback
and recompile and see if it makes a difference.
  댓글 수: 2
Mark Finn
Mark Finn 2020년 12월 9일
Excellent! I've been looking for this everywhere. My callback function could not be found - but only in compiled programs. They worked fine from within Matlab. This pragma did the trick. Thank you!
The docs are at https://www.mathworks.com/help/compiler/function.html#:~:text=%25%23function%20object_constructor-,Description,objects%20loaded%20from%20MAT%2Dfiles.
Mark Finn
Mark Finn 2020년 12월 9일
To help others find this, the errors I was receiving (only when I ran my compiled Matlab program from the command line so I could see error messages) was "Undefined function or variable '<program>_cb'." and then "Error while evaluating UIControl Callback"..

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

추가 답변 (1개)

Jay
Jay 2013년 7월 31일
Thanks for the suggestion, this worked!

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by