0
votes

this is my Matlab code, I want to read .wav file from the same file or another file.

str=['1.wav';'2.wav';'3.wav';'4.wav';'5.wav';];
for i=1:5
    [y, fs]=wavread(str(i));
    a = miraudio(str(i));
    z = mirzerocross(a)
    close all
end

it gives me error like.. Error using TRYFINAL (line 1) Error using vertcat Dimensions of matrices being concatenated are not consistent.

1
Maybe write str=['1.wav';'2.wav';'3.wav';'4.wav';'5.wav'];? The semicolon is the vertical concatenation operator; no need to use it at the end. - A. Donda
Also, str(i) will give you a single char, not the full file name. Put it in a cell array instead and use str{i}. - nkjt
same error without ; at the end - vishwa joshi
str{i} is working thank you , but if i want to fetch .wav file directly from folder or any other .m file one by one or in for loop , how to do it?? - vishwa joshi
I would recommend using the .name field of the items in the structure returned by dir with fullfile in your wavread call - excaza

1 Answers

1
votes

Your OP is failing because of how MATLAB character arrays are implemented (@patrik has a very good explanation in this recent question). If you want to use a character array every row must be the same length, requiring you to pad the entries somehow which, while doable, isn't very efficient. The alternative is to use cell arrays, as @nkjt suggested, which will work for the implementation outlined in your OP.

A more general approach, however, is to use the data structure returned by MATLAB's dir command to identify all of the *.wav files in a directory and perform some operation on all of them.

pathname = 'C:\somewavfiles'; % Full path to a folder containing some wav files

wavfiles = dir(fullfile(pathname, '*.wav')); % Obtain a list of *.wav files

% Loop over all the files and perform some operations
for ii = 1:length(wavfiles)
    filepath = fullfile(pathname, wavfiles(ii).name); % Generate the full path to the file using the filename and the pathname specified earlier
    [y, fs] = wavread(filepath);
    a = miraudio(filepath);
    z = mirzerocross(a);
end

I have used fullfile in a few places rather than concatenating strings with a slash in order to avoid compatibility issues between operating systems. Some use \ and others use /.

Also note that, as explained by the documentation, you can use wildcards (*) in dir calls to narrow down the list of files returned.