How to pass audio file name to GUI and have GUI play that file?

16 ビュー (過去 30 日間)
aburl 2018 年 7 月 19 日
コメント済み: aburl 2018 年 8 月 10 日
I'd like to pass a filename from a .m file to the GUI, and then have the GUI play that audio file. I'd rather send the audio filename over rather than code anything into the GUI because: 1) The GUI will play 21 iterations of different files that are predetermined 2) that predetermined iteration is random at each startup and needs to be saved outside of the GUI.
Eventually, the GUI will ask users to compare two different audio files, but right now I can't get it to even play one (which is where I need your help).
Here's (part of) the main script:
cueOrder = nchoosek([0.0, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5],2);
randomOrder = randperm(size(cueOrder,1));
cueOrder = cueOrder(randomOrder,:); %randomly organizes rows together
i = 1;
while i==1 %i<=21 (temporary until gui runs properly)
%name reverberation time
reverbTime1 = num2str(cueOrder(i,1));
% zero pad if necessary to get the proper name
if length(reverbTime1)==1
reverbTime1 = [reverbTime1,'.0'];
stimulusFile1 = ['\\*****\***Lab\Andrew\Music Stim Example\Stimuli\Beet_',reverbTime1,'.wav'];
%this is supposed to be used to send file location information to the GUI but it won't work for some reason
Here's the GUI script. I'm less sure what's going on here... so I included a lot more of the code:
function varargout = cuepreference(varargin)
% Last Modified by GUIDE v2.5 17-Jul-2018 13:29:37
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @cuepreference_OpeningFcn, ...
'gui_OutputFcn', @cuepreference_OutputFcn, ...
'gui_LayoutFcn', [], ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
gui_mainfcn(gui_State, varargin{:});
% End initialization code - DO NOT EDIT
if nargin == 0; %waiting here for start press
fig = openfig(mfilename,'reuse');
% Generate a structure of handles to pass to callbacks, and store it.
handles = guihandles(fig);
guidata(fig, handles);
% Wait for start button callback
handles = guidata(fig);
varargout{1} = handles;
% we got here by pushing the start button
elseif strcmp(varargin{1},'StartButton_Callback')==1
fig = openfig(mfilename,'reuse');
handles = guidata(fig);
guidata(fig, handles);
%pull in parameters from main script - this isn't working
stimulusFile1 = varargin{1}.stimulusFile1;
[stimulus1,fs1] = audioread(stimulusFile1);
elseif strcmp(varargin{1},'QuitButton_Callback')==1
fig = openfig(mfilename,'reuse');
handles = guidata(fig);
guidata(fig, handles);
close all
% --- Executes just before cuepreference is made visible.
function cuepreference_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin unrecognized PropertyName/PropertyValue pairs from the
% command line (see VARARGIN)
% Choose default command line output for cuepreference
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes cuepreference wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = cuepreference_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in PlayButton1.
function PlayButton1_Callback(hObject, eventdata, handles)
% hObject handle to PlayButton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
guidata(hObject, handles);
These are the errors I get. My script is called test.m and I have it tell me when buttons are pressed in the cuepreference.m figure (start, play1, quit).
>> test
Attempt to reference field of non-structure array.
Error in cuepreference (line 82)
stimulusFile1 = varargin{1}.stimulusFile1;
Error in @(hObject,eventdata)cuepreference('StartButton_Callback',hObject,eventdata,guidata(hObject))
Error using waitfor
Error while evaluating UIControl Callback
Undefined function or variable 'stimulus1'.
Error in cuepreference>PlayButton1_Callback (line 185)
Error in gui_mainfcn (line 95)
Error in cuepreference (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)cuepreference('PlayButton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
I'm very new to MATLAB and a lot of this was cobbled together from other projects in our lab, put together with my limited understanding of programming. I'm happy to include more of either the script or the figure code if anybody would like.
I'm guessing I'm missing something fundamental here and I can't see it!

回答 (1 件)

Image Analyst
Image Analyst 2018 年 7 月 20 日
Did you try sound() or soundsc() to play your data?
  4 件のコメント
aburl 2018 年 8 月 10 日
Hi there - I was wondering if you had gotten a chance to take a look at this or not (or if it's just too messy to work with).
Obviously no pressure or obligation! Just curious as I'm still stuck (I'm missing something fundamental obviously).


Community Treasure Hunt

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

Start Hunting!

Translated by