How to pass audio file name to GUI and have GUI play that file?
7 views (last 30 days)
Show older comments
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'];
end
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
filestrings.stimulusFile1=stimulusFile1;
handles=cuepreference(filestrings);
i=i+1;
end
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});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% 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
uiwait(fig)
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
end
% --- 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)
sound(stimulus1,fs1);
disp('play1')
guidata(hObject, handles);
uiresume(handles.figure1);
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
start
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
play1
Undefined function or variable 'stimulus1'.
Error in cuepreference>PlayButton1_Callback (line 185)
sound(stimulus1,fs1);
Error in gui_mainfcn (line 95)
feval(varargin{:});
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
quit
>>
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!
0 Comments
Answers (1)
See Also
Categories
Find more on Code Generation and Deployment in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!