Audio player won't work with structure error when running callback

1 Ansicht (letzte 30 Tage)
Ok, so I have looked at other problems concerning this error and none of them apply to mine. I am trying to include a simple music player into my program in MATLAB, but every time I try to run it I get the error:
Attempt to reference field of non-structure array.
Error in Beam_Deflection_GUI_3_Music>Play_Music_Call (line 388)
n = get(S.listMusic,'Value');
Error while evaluating uicontrol Callback
I have now figured out that somehow I am turning the S structure into a double, but I do not know how I am doing that, or even how one would go about doing that. If it would help here is the code:
MusicChoice = {'Message in A Bottle','Roxanne'};
S.Pa4 = uipanel('title','Music',...
'FontSize',12,...
'BackgroundColor','white',...
'Units','pixels',...
'Position',[25 80 280 425],...
'Parent',S.fh,...
'fontweight','b',...
'FontAngle','italic',...
'visible','off');
S.listMusic = uicontrol('parent',S.Pa4,...
'style','popupmenu',...
'String',MusicChoice);
S.Play = uicontrol('parent',S.Pa4,...
'style','push',...
'string','Play',...
'units','pix',...
'pos',[100 100 20 20],...
'callback',@Play_Music_Call);
function [] = Play_Music_Call(varargin)
S = varargin{1};
n = get(S.listMusic,'Value');
MusicChoice = {'Message in A Bottle','Roxanne'};
mChoice = MusicChoice(n,1);
[y, Fs, nbits] = wavread(mChoice);
S.player = audioplayer(y, Fs, nbits);
play(S.player)
end

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 12 Dez. 2012
When you have a callback, two arguments are always passed to it: source, and event. Source is the handle of the object that generated the callback, and event has details about the event that was triggered. No other arguments are passed by default: any other arguments must be passed explicitly. When you are using GUIDE, GUIDE generates the callback to not name a routine directly and instead to execute code that finds the "handles" structure and pass it as a third argument to the callback routine.
Thus, varargin{1} in your routine is the handle to the uicontrol (same as S.play). handles are datatype "double".
It looks to me as if Play_Music_Call is possibly a nested function. If it is not nested at the moment, you could convert it to nested by adding an "end" after the Play_Music_Call definition
function Beam_Deflection_GUI_3_Music
[....]
function Play_Music_Call
[...]
end
end
Once it is nested, then remove the assignment to S in Play_Music_Call, and then that routine will have access to the values assigned to S in Beam_Deflection_GUI_3_Music

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by