listbox selection, push button and which error..
2 次查看(过去 30 天)
显示 更早的评论
Hey all,
I have a gui listbox that loads only wav file names into it on the gui opening. From there a push button takes the selection and audioreads the chosen file. Well it should do but with the following code, it errors out. See error below below..
% **********************************************
% FORM LOAD AND OPEN..
% **********************************************
% --- Executes just before EPM_gui_form is made visible.
function EPM_gui_form_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for EPM_gui_form
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% DECLARE GLOBAL VARIABLES..
%global isMELODY; % DEFINE isMELODY AS CATCH FOR IF MELODY IS CREATED OR NOT..
%isMELODY = 0; % SET isMELODY TO 0 (NOT CREATED YET)..
global NoteX; % DECLARE VARIABLE FOR NOTE LETTER..
global FreqX; % DECLARE VARIABLE FOR NOTE FREQUENCY..
global FreqBPFTStop1; % DECLARE VARIABLE FOR BP FILTER STOPBAND 1..
global FreqBPFTPass1; % DECLARE VARIABLE FOR BP FILTER PASSBAND 1..
global FreqBPFTPass2; % DECLARE VARIABLE FOR BP FILTER PASSBAND 2..
global FreqBPFTStop2; % DECLARE VARIABLE FOR BP FILTER STOPBAND 2..
global FreqBSFTStop1; % DECLARE VARIABLE FOR BS FILTER STOPBAND 1..
global FreqBSFTPass1; % DECLARE VARIABLE FOR BS FILTER PASSBAND 1..
global FreqBSFTPass2; % DECLARE VARIABLE FOR BS FILTER PASSBAND 2..
global FreqBSFTStop2; % DECLARE VARIABLE FOR BS FILTER STOPBAND 2..
global d; % DECLARE VARIABLE FOR DIRECTORY..
global strLOAD; % DECLARE VARIABLE FOR FILENAMES..
% LOAD WAV FILES INTO LISTBOX..
d = dir('*.wav'); % Select Only ‘.wav’ Files
strLOAD = {d.name};
set(handles.listFILEwav,'String',strLOAD); %set string
% **********************************************
% **********************************************
% FORM OUTPUT..
% **********************************************
% --- Outputs from this function are returned to the command line.
function varargout = EPM_gui_form_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% **********************************************
% **********************************************
% LOAD WAV..
% **********************************************
% --- Executes on button press in btLOAD.
function btLOAD_Callback(hObject, eventdata, handles)
global strLOAD; % DECLARE VARIABLE FOR FILENAMES..
wav_name = strLOAD; % FILE NAME OF THE SELECTED PATH..
wav_path = which(wav_name); % PATH OF THE SELECTED FILE..
msgbox({'Wav: ',wav_name});
% OPEN AND READ THE DESIRED AUDIO WAV FILE..
[Wave,Fs] = audioread(wav_name);
soundsc(Wave, Fs);
% **********************************************
Here's the error:
Error using which
Argument must contain a string.
Error in EPM_gui_form>btLOAD_Callback (line 89)
wav_path = which(wav_name); % Path Of Selected File
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in EPM_gui_form (line 34)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)EPM_gui_form('btLOAD_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
It says which needs to be a string but I would have thought that the dir kicks out a string of the files and populating the listbox with these strings as strLOAD, so therefore the code in the btLOAD_Callback should be a string? Or am i missing something?
Thanks.
Paul..
0 个评论
采纳的回答
Walter Roberson
2015-11-20
You are incorrect. The pushbutton ignores the listbox rather than taking the selection. Instead the pushbutton is taking the result of
d = dir('*.wav'); % Select Only ‘.wav’ Files
strLOAD = {d.name};
which is creating strLOAD as a cell array of strings rather than as a single string. You cannot which() a cell array of strings.
Using which() is a waste of time. You already know which directory they are in: they are in the directory that you did the dir('*.wav') inside. You can find out which directory you are in by using the call cd()
3 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!