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

23 Ansichten (letzte 30 Tage)
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 Kommentar
Stephen23
Stephen23 am 26 Jan. 2023
Bearbeitet: Stephen23 am 26 Jan. 2023
"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:

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 26 Jan. 2023
Bearbeitet: Stephen23 am 26 Jan. 2023
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

Weitere Antworten (0)

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by