How to load all .mat file in the current directory and save them as .wav file?

25 ビュー (過去 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
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
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 ExchangeFile Operations についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by