MATLAB GUIDE and compiler

6 visualizzazioni (ultimi 30 giorni)
Jay
Jay il 31 Lug 2013
Commentato: Mark Finn il 9 Dic 2020
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'));

Risposta accettata

Matt J
Matt J il 31 Lug 2013
Modificato: Matt J il 31 Lug 2013
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 Commenti
Mark Finn
Mark Finn il 9 Dic 2020
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 il 9 Dic 2020
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"..

Accedi per commentare.

Più risposte (1)

Jay
Jay il 31 Lug 2013
Thanks for the suggestion, this worked!

Categorie

Scopri di più su Entering Commands in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by