Undefined function or variable 'hObject' in app designer

1 次查看(过去 30 天)
[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.

回答(1 个)

Melissa Williams
Melissa Williams 2018-1-24
编辑:Melissa Williams 2018-1-24
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

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by