Using matlab record and save in a specific folder - matlab

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);

Related

How to read video from diffrent folder and creating Video object in matlab?

I have a lot of videos to run which are kept in the diffrent folder than my current directory of Matlab and VideoReader is not taking the directory address of the video. Need help in creating video object of video kept in a diffrent folder.
filePattern = fullfile(pwd, 'videoDir\videoname.mp4');
fileList = dir (filePattern );
video_name =fileList.name;
obj = VideoReader(video_name);
The .name field of the directory structure is only the final part of the name - it does not include any folders or subfolders. Your very first line defines the entire absolute path and filename for the video file. You can pass that to VideoReader directly.
filePattern = fullfile(pwd, 'videoDir\videoname.mp4');
obj = VideoReader(filePattern);
In fact, there's no reason you need the 'fullfile' call unless you are going to want to reference this file from a different directory at some later date.
obj = VideoReader('videoDir/videoname.mp4');
For a more flexible version of this, consider we have a bunch of *.mp4 files in a bunch of sub-directories and we want to step through all of them.
Directory = dir('*/*.mp4'); % this command works on Windows or Linux
for jj = 1:length(Directory)
obj(jj) = VideoReader(fullfile(Directory(jj).folder,Directory(jj).name));
end

saving wav files in 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');

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

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.

how to use save wav files to current subdirectory in matlab?

My current directory is under C, for example, "C:\xxx\"
Now, I want to export my processed wav files to a subfolder in my current directory, for example, "\wav_results\".
What I have done is declaring a filepath variable:
wav_dir = '\wav_results\';
wavwrite(...., [wav_dir wav_name]) %wav_name is the name of the wav file
The error says no such file or directory. I do not want to use the full directory path for wav_dir because I need to move this script from place to place. Anyone has good suggestion?
Thanks~
Use mkdir before you call wavwrite:
wav_dir = '\wav_results\'; %'
if not(exist('testresults','dir'))
mkdir(wav_dir);
end
wavwrite(...., [wav_dir wav_name])