运行回调时音频播放器无法处理结构错误

时间:2022-09-06 17:10:59

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

好的,所以我查看了有关此错误的其他问题,但没有一个适用于我的错误。我试图在MATLAB中将一个简单的音乐播放器包含到我的程序中,但每次我尝试运行它都会出错

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 am not sure why but here is my 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 

1 个解决方案

#1


0  

For completeness so other people dont have to read all the comments first:

为了完整性,所以其他人不必首先阅读所有评论:

The asker posted this as a solution:

提问者将此作为解决方案发布:

Ok, So I found out that when I did S = varargin{1}

好的,所以我发现当我做S = varargin {1}

I was saving the handle, which is a double and not a struct, to S.

我正在将句柄保存到S,这是一个双重而不是结构。

To work around this I had to nest the call back function in my larger Function which created the GUI , Beam_Deflection_GUI_3.

为了解决这个问题,我不得不在我更大的函数中嵌入回调函数,该函数创建了GUI,Beam_Deflection_GUI_3。

I then got rid of the S = varargin{1} from the callback.

然后我从回调中摆脱了S = varargin {1}。

It now works because it can access the S struct without having to re save it.

它现在有效,因为它可以访问S结构而无需重新保存它。

#1


0  

For completeness so other people dont have to read all the comments first:

为了完整性,所以其他人不必首先阅读所有评论:

The asker posted this as a solution:

提问者将此作为解决方案发布:

Ok, So I found out that when I did S = varargin{1}

好的,所以我发现当我做S = varargin {1}

I was saving the handle, which is a double and not a struct, to S.

我正在将句柄保存到S,这是一个双重而不是结构。

To work around this I had to nest the call back function in my larger Function which created the GUI , Beam_Deflection_GUI_3.

为了解决这个问题,我不得不在我更大的函数中嵌入回调函数,该函数创建了GUI,Beam_Deflection_GUI_3。

I then got rid of the S = varargin{1} from the callback.

然后我从回调中摆脱了S = varargin {1}。

It now works because it can access the S struct without having to re save it.

它现在有效,因为它可以访问S结构而无需重新保存它。