How to load all .mat file in the current directory and save them as .wav file?
26 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I want to load all '.mat' files from a folder. All these '.mat' files have two vectors names 'right' and 'left'. I want to save these two vectors as two separate '.wav' files. Here is the code that I used to do it:
clear
close all
clc
%% reading all the files in the current directory
path_directory = 'C:\Users\Desktop\BE';
original_files=dir([path_directory '/*.mat']);
%% loading each file and save them as a wav file
for k=1:length(original_files)
filename=[path_directory '/' original_files(k).name];
load(filename)
wavname_r = filename + '_r.wav';
fs = FS_AUDIO;
sig_r = right;
audiowrite(wavname_r,sig_r,fs);
wavname_l = filename + '_l.wav';
sig_l = left;
audiowrite(wavname_l,sig_l,fs)
end
But it didn't work well and I got this error:
Arrays have incompatible sizes for this operation.
Error in mat_to_wav (line 15)
wavname_r = filename + '.wav';
could you please explain to me how to solve it??
Thank you very much in advance.
1 个评论
Stephen23
2023-1-26
编辑:Stephen23
2023-1-26
"could you please explain to me how to solve it??"
Do not use + on character vectors (unless you really want to add the character codes of compatibly-sized char vectors).
The + operator is overloaded to operate on string arrays, as its documentation explains:
采纳的回答
Stephen23
2023-1-26
编辑:Stephen23
2023-1-26
P = 'C:\Users\Desktop\BE';
S = dir(fullfile(P,'*.mat'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
D = load(F);
[~,N] = fileparts(S(k).name);
FnL = fullfile('.',sprintf('%s_l.wav',N));
audiowrite(FnL, D.left, D.FS_AUDIO);
FnR = fullfile('.',sprintf('%s_r.wav',N));
audiowrite(FnR, D.right, D.FS_AUDIO);
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!