Effacer les filtres
Effacer les filtres

Undefined function or variable 'hObject' in app designer

2 vues (au cours des 30 derniers jours)
Chunhui Guo
Chunhui Guo le 23 Jan 2018
Commenté : Brian le 29 Août 2018
[filename, pathname] = uigetfile('*.mp3', 'Select audiofile');
[a,fs]=audioread([pathname filename]);
handles.B1 = audioplayer(a,fs);
play(handles.B1);
guiData(hObject,handles);
I have this code and it keep telling me that the hObject is undefined. I want to use it for a push button, so that when I click the button, it will let me to select a sound file and play it. I know I need to use guidata function to save it in order to play it. However, I don't really know how to use it, like how to create an hObject in order to make it work properly.

Réponses (1)

Melissa Williams
Melissa Williams le 24 Jan 2018
Modifié(e) : Melissa Williams le 24 Jan 2018
Hi Chunhui, You don't need to use handles or guiData any longer if you are using App Designer. Instead you can store your data on the app as properties. To do this, in Code View in the Toolstrip there is a Insert Section. Push the property button. This will add a property to your code which you could name B1. To reference it in your code, type app.B1 instead of using handles and gui data.
Your new code should look something like this:
Up top after component properties read-only section,
properties (Access = private)
B1 % Audio File to play when button pushed
end
And in your button callback:
[filename, pathname] = uigetfile('*.mp3', 'Select audiofile');
[a,fs]=audioread([pathname filename]);
app.B1 = audioplayer(a,fs);
play(app.B1);
Let me know if this works for you. -Melissa
  1 commentaire
Brian
Brian le 29 Août 2018
thank you! I've been looking for this for hours!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by