Help looping through mat files with sound function

조회 수: 3 (최근 30일)
Daulton_Benesuave
Daulton_Benesuave 2016년 1월 1일
댓글: Walter Roberson 2016년 1월 29일
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일
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월 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개)

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by