Clear Filters
Clear Filters

Undefined function or variable 'hObject' in app designer

2 views (last 30 days)
[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.

Answers (1)

Melissa Williams
Melissa Williams on 24 Jan 2018
Edited: Melissa Williams on 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

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!