Convert csv file to .wav file with same file name

12 views (last 30 days)
Hi,
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(file.name);
[filepath,name,ext] = fileparts(file.name);
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);
clearvars
end
  2 Comments
Walter Roberson
Walter Roberson on 2 Sep 2023
My code in my Answer shows converting a directory of csv files to corresponding wav files, under the assumption of particular minimum and maximums and particular recording rate.
The difference for xls or xlsx files would just be changing the '*.csv' to the appropriate file extension.
The input minimums and maximums and the sample rate would have to be adjusted for your situation.

Sign in to comment.

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, file.name);
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);
end

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!