Help looping through mat files with sound function

I have several *.mat sound files (e.g., tunes_audio.mat) that I have load into my Matlab workspace using the following code:
s = what;
matfiles=s.mat;
for a=1:numel(matfiles)
load(char(matfiles(a)))
end
I would now like to play each of these files using the sound function. Simply typing sound(tunes_audio) works just fine. But I want to loop through all the loaded files and play them one at a time without having to type each name into the script. I have tried sound(char(matfiles{1}(1:end-4))) but I get an error message saying "Audio data must be real and floating point." Would somebody please help me loop through and read each loaded *.mat file?
Thank you in advance!

 採用された回答

Walter Roberson
Walter Roberson 2016 年 1 月 1 日

1 投票

dinfo = dir('*.mat');
for K = 1 : length(dinfo)
thisfile = dinfo(K).name;
datastruct = load(thisfile);
fn = fieldnames(datastruct);
data = datastruct.(fn{1});
fprintf('now playing: %s\n', thisfile);
sound(data);
end

6 件のコメント

Daulton_Benesuave
Daulton_Benesuave 2016 年 1 月 1 日
Thanks. I tried your solution but still received the same error
Error using sound (line 33) Audio data must be real and floating point.
Walter Roberson
Walter Roberson 2016 年 1 月 1 日
dinfo = dir('*.mat');
for K = 1 : length(dinfo)
thisfile = dinfo(K).name;
datastruct = load(thisfile);
fn = fieldnames(datastruct);
data = datastruct.(fn{1});
bad_data = true;
if ~isfloat(data)
fprintf('Ooo... file %s variable %s is not floating point, is %s skipping it\n', thisfile, fn{1}, class(data));
elseif ~isreal(data)
fprintf('Ooo... file %s variable %s is floating point but complex, skipping it\n', thisfile, fn{1});
elseif isempty(data)
fprintf('Ooo... file %s variable %s is real floating point but empty, skipping it\n', thisfile, fn{1});
else
bad_data = false;
end
if bad_data
for J = 2 : length(fn)
data2 = datastruct.(fn{J});
if ~isempty(data2) & isfloat(data2) & isreal(data2)
fprintf('But file %s variable %s looks okay, perhaps you want to load it.\n', thisfile, fn{J});
end
end
else
fprintf('now playing: %s variable %s\n', thisfile, fn{1});
sound(data);
end
end
Daulton_Benesuave
Daulton_Benesuave 2016 年 1 月 2 日
編集済み: Walter Roberson 2016 年 1 月 2 日
Walter,
Than you for trying to help. I ran the code and below is the output.
Ooo... file mule_audio.mat variable ans is not floating point, is audioplayer skipping it
But file mule_audio.mat variable mule_audio looks okay, perhaps you want to load it.
I am afraid I do not know enough about Matlab to decipher the results and hence I cannot get the files to play ...
Walter Roberson
Walter Roberson 2016 年 1 月 2 日
編集済み: Walter Roberson 2016 年 1 月 2 日
dinfo = dir('*.mat');
for K = 1 : length(dinfo)
thisfile = dinfo(K).name;
datastruct = load(thisfile);
fn = fieldnames(datastruct);
mask = ismember(fn, {'ans'}) );
if any(mask)
fprintf('Skipping file "%s" variable "ans"\n', thisfile);
fn(mask) = [];
end
data = datastruct.(fn{1});
bad_data = true;
if ~isfloat(data)
fprintf('Ooo... file "%s" variable "%s" is not floating point, is "%s" skipping it\n', thisfile, fn{1}, class(data));
elseif ~isreal(data)
fprintf('Ooo... file "%s" variable "%s" is floating point but complex, skipping it\n', thisfile, fn{1});
elseif isempty(data)
fprintf('Ooo... file "%s" variable "%s" is real floating point but empty, skipping it\n', thisfile, fn{1});
else
bad_data = false;
end
if bad_data
for J = 2 : length(fn)
data2 = datastruct.(fn{J});
if ~isempty(data2) & isfloat(data2) & isreal(data2)
fprintf('But file "%s" variable "%s" looks okay, perhaps you want to load it.\n', thisfile, fn{J});
end
end
else
fprintf('now playing: file "%s" variable "%s"\n', thisfile, fn{1});
sound(data);
end
end
Daulton_Benesuave
Daulton_Benesuave 2016 年 1 月 28 日
編集済み: Daulton_Benesuave 2016 年 1 月 28 日
Thank you. The sound does now play but it's extremely distorted.
Walter Roberson
Walter Roberson 2016 年 1 月 29 日
You probably need to specify a sampling frequency in the sound() call, but you have not given us any way to know what the appropriate frequency would be.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeAudio I/O and Waveform Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by