Convert csv file to .wav file with same file name

조회 수: 15 (최근 30일)
Giggs B.
Giggs B. 2022년 3월 16일
댓글: Walter Roberson 2023년 9월 2일
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
RAGHUNATH
RAGHUNATH 2023년 9월 2일
How to convert exsl(.csv) file to wav file
Walter Roberson
Walter Roberson 2023년 9월 2일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2022년 3월 16일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by