How to load all .mat file in the current directory and save them as .wav file?
25 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Malahat Hajkarimi Mehrban
il 26 Gen 2023
Commentato: Malahat Hajkarimi Mehrban
il 27 Gen 2023
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 Commento
Stephen23
il 26 Gen 2023
Modificato: Stephen23
il 26 Gen 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:
Risposta accettata
Stephen23
il 26 Gen 2023
Modificato: Stephen23
il 26 Gen 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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su File Operations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!