How can I convert a .wav file to .txt file with format mentioned below?

12 Ansichten (letzte 30 Tage)
Allen Borrow
Allen Borrow am 16 Jul. 2022
Bearbeitet: Stephen23 am 16 Jul. 2022
I've been able to convert the .wav file into .txt and save it to a .txt file of the same name but am having trouble with text formatting. It shows up as a giant blob of numbers rather than a list like I'd hoped, and the .*1000 doesn't work for the first few hundred numbers for some reason. Would love help to figure out the formatting issues!
The current output I've is the .txt file attached and the code I have is also attached
  2 Kommentare
Allen Borrow
Allen Borrow am 16 Jul. 2022
Seems that changing %*.2f to %*.2f\n solves the first issue
Allen Borrow
Allen Borrow am 16 Jul. 2022
Edit: It looks like the formatting is only getting messed up for bigger numbers. No idea why

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 16 Jul. 2022
Bearbeitet: Stephen23 am 16 Jul. 2022
Your code has a few issues to be fixed:
  • as you noticed, if you want any characters to be printed between those numbers then you need to specify those characters. FPRINTF cannot not guess that you want a newline character between those numbers, you have to tell it.
  • Your code does not FCLOSE the file, which is likely to also cause you problems. Whenever you FOPEN a file you must always FCLOSE that file when you have finished.
  • You included an asterisk in the format string, but it is unclear why you did this and it will likely mess up your formattng exactly as you describe. An asterisk allows the user to specify things like the field width and precision using an input argument, rather than hard-coding it in the format string. So your provided array will be interpreted as pairs of [fieldwidth,dataval], which is very unlikely to be what your WAV data represent. See also: https://www.mathworks.com/help/matlab/ref/fprintf.html#btf8xsy-1_sep_shared-formatSpec
[F,P] = uigetfile('*.wav');
V = audioread(fullfile(P,F));
[~,G] = fileparts(F);
G = sprintf('%s.txt',G);
Using FPRINTF and remembering to FCLOSE:
fid = fopen(G,'wt');
fprintf(fid,'%.2f\n',V*1000);
fclose(fid);
But unless you really want to mess around with FPRINTF using a high-level function would be much easier, e.g.:
writematrix(V*1000,G)

Community Treasure Hunt

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

Start Hunting!

Translated by