How to save multiple file (.mat) into .wav using MATLAB - matlab

I have a .mat file containing 100files. How to convert the 100 files one by one to .wav.
Every file contain vectors.I tried using this code but I got errors.
x=load('data_cropped.mat');
input_list = x;
for i = 1:length(input_list)
fid = fopen(input_list(i).name);
data = ' ';
fopen(fid);
wavwrite(data,16000,[input_list(i).name(1:length(input_list(i).name)-3),'wav']);
clear data
end
The error is:
>> convert_to_wav
Reference to non-existent field 'name'.
Error in convert_to_wav (line 7)
fid = fopen(input_list(i).name);
Please help me,
Thanks a lot

Assuming you know the sample rate of your audio then the snippet below should do what you want and give you a series of numbered wav files.
clear
load('data_cropped.mat');
data = whos;
fs = 44100 %change to your sample rate
for i = 1:length(data)
wavwrite(data(i).name,fs,num2str(i));
end

Related

How can I use the data received as readtable as input of audioread? (MATLAB)

I have 30000 aiff files named audio1 ~ 30000.
And there is a csv file consisting of selected file names among them. (About 7000)
I want to audioread only the aiff data in the list of csv files.
I applied the audioread function immediately after receiving the csv file with the readtable function, but received an error message that only a string or char type vector can come as the input of the audioread.
Below is my short code.
Please help. Thanks.
clc;close all;clear all;
T = readtable('train_py.csv');
fs = 2000; %All aiff files have a sampling frequency of 2000 Hz.
y = zeros(length(T), fs);
for i = 1:length(T)
y(i,:) = audioread(T(i));
end

reading decimal number with GUI matlab

I am developing a user interface using matlab wich allows to browse and load a text file and display some curves. I am facing a problem, my file text is a set of decimal number, matlab is reading those number as two columns.
this is an exemple: u find here the file that I am working on:
After runing this code :
[filename pathname] = uigetfile({'*.txt'},'File Selector');
fullpathname = strcat(pathname,filename);
text = fileread(fullpathname); %reading information inside a file
set(handles.text6,'string',fullpathname)%showing full path name
set(handles.text7,'string',text)%showing information
loaddata = fullfile(pathname,filename);
xy = load(loaddata,'-ascii','%s');
t = xy(:,1);
i = xy(:,3);
handles.input1 = i;
handles.input2 = t;
axes(handles.axes1);
plot(handles.input1,handles.input2)
the curves looks so strenge, so I checked the result of xy= load(loaddata,'-ascii') using command window and here the problem appears!
So I have now 12 columns instead of 6 ! can u help me please?
I tried with strrep(data,',','.') but it doesnt work !
Since you are using commas, for your radix point, you will want to first load in the entire file as a string, replace the , with . and then you can use str2num to convert the entire file to a numeric array
% Read the entire file into memory
fid = fopen(loaddata, 'rb');
contents = fread(fid, '*char')';
fclose(fid);
% Replace `,` with `.`
contents = strrep(contents, ',', '.');
% Now convert to numbers
data = str2num(contents);

MATLAB File I/O

I am trying to read in a .txt file that is a matrix and turn it into a row vector. However, I keep getting the error "Empty format string is not supported at the end of a file." Here is my code. Can anyone explain why I'm getting this?
filename = 'matrix.txt';
fID = fopen(filename);
M = csvread(filename);
rowVector = M(:);

Importing large amount of data into MATLAB?

I have a text file that is ~80MB. It has 2 cols and around 6e6 rows. I would like to import the data into MATLAB, but it is too much data to do with the load function. I have been playing around with the fopen function but cant get anything to work.
Ideally I would like to take the first col of data and import and eventually have it in one large array in MATLAB. If that isn't possible, I would like to split it into arrays of 34,013 in length. I would also like to do the same for the 2nd col of data.
fileID = fopen('yourfilename.txt');
formatSpec = '%f %f';
while ~feof(fileID)
C = textscan(fileID,formatSpec,34013);
end
Hope this helps..
Edit:
The reason you are getting error is because C has two columns. So you need to take the columns individually and handle them.
For example:
column1data = reshape(C(:,1),301,113);
column2data = reshape(C(:,2),301,113);
You may also consider to convert your file to binary format if your data file is not changing each time you want to load it. Then you'll load it way much faster.
Or you may do "transparent binary conversion" like in the function below. Only first time you load the data will be slow. All subsequent will be fast.
function Data = ReadTextFile(FileName,NColumns)
MatFileName = sprintf('%s.mat',FileName); % binary file name
if exist(MatFileName,'file')==2 % if it exists
S = load(MatFileName,'Data'); % load it instead of
Data = S.Data; % the original text file
return;
end
fh = fopen(FileName); % if binary file does not exist load data ftom the original text file
fh_closer = onCleanup( #() fclose(fh) ); % the file will be closed properly even in case of error
Data = fscanf(fh, repmat('%f ',1,NColumns), [NColumns,inf]);
Data = Data';
save(MatFileName,'Data'); % and make binary "chache" of the original data for faster subsequent reading
end
Do not forget to remove the MAT file when the original data file is changed.

How to read numbered sequence of .dat files into MATLAB

I am trying to load a numbered sequence of ".dat" named in the form a01.dat, a02.dat... a51.dat into MATLAB. I used the eval() function with the code below.
%% To load each ".dat" file for the 51 attributes to an array.
a = dir('*.dat');
for i = 1:length(a)
eval(['load ' a(i).name ' -ascii']);
end
attributes = length(a);
I ran into problems with that as I could not easily manipulate the data loaded with the eval function. And I found out the community is strongly against using eval. I used the csvread() with the code below.
% Scan folder for number of ".dat" files
datfiles = dir('*.dat');
% Count Number of ".dat" files
numfiles = length(datfiles);
% Read files in to MATLAB
for i = 1:1:numfiles
A{i} = csvread(datfiles(i).name);
end
The csvread() works for me but it reads the files but messes up the order when it reads the files. It reads a01.dat first and then a10.dat and a11.dat and so on instead of a01.dat, a02.dat... The contents of each files are signed numbers. Some are comma-delimited and single column and this is an even split. So a01.dat's contents are comma-delimited and a02.dat's content are in a single column.
Please how do I handle this?
Your problem seems to be sorting of the files. Drawing on a question on mathworks, this should help you:
datfiles = dir('*.mat');
name = {datfiles.name};
[~, index] = sort(name);
name = name(index);
And then you can loop with just name:
% Read files in to MATLAB
for i = 1:1:numfiles
A{i} = csvread(name{i});
end