MATLAB Answers

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

Asked by Lawson Hoover on 12 Dec 2012

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',...            
          'Position',[25 80 280 425],...
S.listMusic = uicontrol('parent',S.Pa4,...
S.Play = uicontrol('parent',S.Pa4,...
    'pos',[100 100 20 20],...
   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);



No products are associated with this question.

1 Answer

Answer by Walter Roberson
on 12 Dec 2012
 Accepted answer

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 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

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

  1 Comment

Lawson Hoover on 12 Dec 2012

Thank you so much!

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today