Convert csv file to .wav file with same file name

11 views (last 30 days)
I think this would be very simple, but I am unable to pass the csv file name to my .wav file. My plan is to read a csv file, cconvert it into a wav file and store it in another folder with the same file name as csv. But I am unable to pass the filename. In my code, inside 'audiowrite()' I provided 'name' thinking that this 'name' will be taken from the 'fileparts()' function, instead it just creates a new file with name as 'name.wav'!
I know the path I provided is a "fixed CHAR vector" so it can't actually get the actual name of the csv file. Then how can I do this? Thanks.
files = dir('*.csv');
for file = files'
n = readmatrix(;
[filepath,name,ext] = fileparts(;
m = rescale(n, -1, 1, 'InputMin',2301,'InputMax',3642)+0.527;
audiowrite('C:\Users\gagan\Downloads\testing_lab\sound files\name.wav',m,40000,'BitsPerSample',16);

Accepted Answer

Walter Roberson
Walter Roberson on 16 Mar 2022
indir = '.'; %were are the csv? %current directory
outdir = 'C:\Users\gagan\Downloads\testing_lab\sound files'; %where to write the results
files = dir( fullfile(indir, '*.csv'));
for file = files'
inname = fullfile(file.folder,;
n = readmatrix(inname);
m = rescale(n, -1, 1, 'InputMin',2301,'InputMax',3642)+0.527;
[filepath,name,ext] = fileparts(inname);
outname = fullfile(outdir, name + ".wav");
audiowrite(outname, m, 40000, 'BitsPerSample', 16);

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by