saving wav files in matlab - matlab

I have a code in matlab where voice is recorded and saved as .wav file with name say.wav.
But the problem which i am facing is, each time i run the code the .wav file gets rewritten. But I want the voice to be recorded into a new .wav file. How can i do this in matlab?
The code is:
Fs = 1E+4;
nBits = 24;
nChannels = 1;
sig = audiorecorder(Fs, nBits, nChannels);
recordblocking(sig,5);
sigsound = getaudiodata(sig);
t= linspace(0, size(sigsound,1), size(sigsound,1))/Fs;
cd F:\1hp_laptop\c\my_files
filename = 'say.wav';
audiowrite(filename, sigsound, Fs)

It is getting rewritten because you have used the constant file name.You need to make your .wav file unique to ensure that it is getting newly created. You can add current time in milliseconds to the file name to make it unique .

As Nilu said, your problem is the filename is constant in your script/function.
One option, as stated, would be to use some kind of timestamp, e.g. instead of
filename = 'say.wav';
you can use
filename = ['say_', datestr(now,'FFF'), '.wav'];
Alternatively, and depending upon your audio file length (if it is long enough), you can ask the user for a distinctive filename, being it by encapsulating all your code into a function and asking for a string to use as parameter filename or by using Matlab's input():
filename = input('give me a filename: ', 's');

Related

Using matlab record and save in a specific folder

I'm recording a sound and using wavwrite to save the wav file, but I need to save it in a specific folder in C:, such as in c:\monitoringsystem.
That's a part of my code:
format shortg
c = clock;
fix(c);
a=num2str(c);
year=strcat(a(1),a(2),a(3),a(4),a(5));
month=strcat(a(19),a(20));
day=strcat(a(34),a(35));
hour=strcat(a(48),a(49));
min=strcat(a(63),a(64));
sec=strcat(a(74),a(75));
name=strcat(year,'-',month,'-',day,'-',hour,'-',min,'-',sec);
wavwrite(y,44100,name);
y=[];
You can include the file path in the name of your sound.
name=strcat(year,'-',month,'-',day,'-',hour,'-',min,'-',sec);
fullpath = fullfile('C:\MYFOLDER',name);
wavwrite(y,44100,fullpath);

MATLAB: Use a variable with a file extenstion to load files

I am using MATLAB
I Have 51 files in their own directory all of .out extention created by a seperate program, all numbered 0 to 50.
ie
0.out
1.out
2.out
and so on til 50.out.
I need to load each file 1 by one to do calculations upon them within a for loop. How would I do this using the count variable to load the file, if the directory is set beforehand?
i.e.
%set directiory
cd(......)
%for loop
For count = 0:50,
data = count.out *<-----this line*
.....
Many thanks!
First generate the file name with
fileName = [int2str(count) '.out'];
then open the file with
fid = fopen(fileName, 'r');
The loading phase depends on the kind of file you want to read. Assuming it is a text file you can, for example, read it line after line with
while ~feof(fid)
line = fgetl(fid);
end
or use more specialized functions (see http://www.mathworks.it/it/help/matlab/text-files.html). Before the end of the for loop you'll have to close the file by calling
fclose(fid);
Another quite nice way to do it is to use the dir function
http://www.mathworks.co.uk/help/matlab/ref/dir.html
a = dir('c:\docs*.out')
Will give you a structure containing all the info about the *.out files in the directory you point it to, (or the path). You can then loop through it bit by bit. using fopen or csvread or whatever file reading function you want to use.

Saving figure with current file name in MatLab

I have a script that pulls one file at a time from my current working directory and plots specified information. I would like to save each of the plots as a jpeg (tiff is okay too) with the name of the file it's plotting. I have about 3000 files, so I am looking for an automated way to do this.
I thought this might work if placed at the end of the for-loop:
saveas(gcf, ' ',jpg)
I am not sure what to put in quotations for the file name.
Example
The plot of the data in data1.mat should be saved in the file data1.jpeg
If loadedFileName is the name (and perhaps) path of the file that you just loaded, then you could do something like the following to save the jpeg with the same file name
% get the path, name and extension of the file just loaded
[path, name, ext] = fileparts(loadedFileName);
% grab what you want to create the filename of the jpeg to save
jpegToSaveFileName = [name '.jpg']; % use path if you want to save to same directory
% save the figure as a jpeg
saveas(gcf,jpegToSaveFileName,'jpg');
Try the above and see what happens. If you need to add a path to the file name, then do something like
jpegToSaveFileName = fullfile(path, jpegToSaveFileName);
Try the above and see if it does what you need!
Since your script already has the information of the filenames (otherwise it could not open the files and read the data), you could just extend the filename with '.jpg' and pass this string to the saveas function. Demo for filename 'hello':
>> filename = 'hello'
filename =
hello
>> picname = [filename, '.jpg']
picname =
hello.jpg
>> a = figure
a =
4
>> saveas(a, picname)
>> ls
hello.jpg

How can I save videos as .MAT files with their names in matlab?

I have a data set of big number of videos so, I want to read these videos and save each video separately with its name because it consumes a lot of time to process among all these videos every time specially for training and classification. If you have any idea how can read all video files in the specified folder D:\words of format .avi and save each one with its own name as .MAT file.
But this code doesn't work
Thanks,,,
files = fuf('D:\words');
for i = 1:size(files);
name = files{i};
file = strcat('D:\words',name);
x = VideoReader(file.avi); %NOT SURE FROM THIS LINE%
v = read(x)
name = strcat(name,'.mat');
save(name,'v');
end
You don't need an additional function like fuf to get a list of file names.
If all your files are in "D:\words" (i.e. not in a bunch of sub-directories, which would complicate things), you can just use things like ls to fetch a list of all the avi files.
This is not the most elegant way of doing it (hard coding the directory and not using things like fullfile), but hopefully it's relatively easy to understand what's going on:
% use ls or dir to specifically match *.avi files
files = ls('D:\words\*.avi')
% note that size can return more than one value
% hence size(files,1)
for n = 1:size(files,1);
filename = files(n,:); % pick one file
% assuming this works - you might want to do some error checking
x = VideoReader(filename);
v = read(x);
% now we just want the name minus the ext
[pathstr,name,ext] = fileparts(filename);
fout = ['D:\words\',name,'.mat'];
save(fout,'v');
end
Your variable file is likely a string, not a structure:
...
file = strcat('D:\words',name);
x = VideoReader(file);
...
Or maybe this if the files in your cell array don't have extensions:
...
file = strcat('D:\words',name);
x = VideoReader([file '.avi']);
...
If your fuf function returns files that are not AVI movies, you'll need to do more work.

batch converting mp4 to avi

I'm trying to convert a bunch of mp4 files to avis using the code below. I know that the conversion works as I've verified it on single files. However when I try to read in all mp4s from a directory the 'files' struct I've tried to create is empty. I'm relatively new to programming in MatLab so I'm sure I'm making many silly errors. How can I properly load in and iterate over multiple mp4 files in order to convert them all to avis? Thanks!
NB: I have tried different methods of conversion, but MatLab seems to be the best at ensuring no size or quality distortion.
files = dir(fullfile('E:\Heather\Summer2013\*.mp4'));
for i = 1:length(files)
filename = files(i).name;
%create objects to read and write the video
readerObj = VideoReader('files(i).name.mp4');
writerObj = VideoWriter('files(i).name.avi','Uncompressed AVI');
%open AVI file for writing
open(writerObj);
%read and write each frame
for k = 1:readerObj.NumberOfFrames
img = read(readerObj,k);
writeVideo(writerObj,img);
end
close(writerObj);
end
A call to dir produces a struct array where file names are stored as strings in the name field. That is, files(i).name contains a string. To use it,
readerObj = VideoReader(files(i).name); % no quotes, no extension
To write with a different file extension, determine the file base with fileparts and add the .avi extension. For example:
>> str = 'mymovie.mp4';
>> [~,fileBase,fileExt] = fileparts(str)
fileBase =
mymovie
fileExt =
.mp4
>> writerObj = VideoWriter([fileBase '.avi'],'Uncompressed AVI');
You are trying to read the file with the name ('files(i).name.mp4') which obviously not exists. filename should be the corret parameter for the reader, for the writer you have to replace the extension.