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.
Related
My matlab function is in a folder that contains the main project and the other functions of the code. However, the data is stored in a folder withing the main one named "data" and inside the specific dataset that i want, for example "ded4" in this example. I can't figure out how to open the text file that I want without changing the file to the main folder. The code I have so far is:
function[Classify] = Classify(logDir)
%%%%logDir='ded014a04';
Directory = ['data/' logDir '/']
Filename = [logDir '-fixationsOffSet']
File_name = fullfile(Directory,Filename)
File = fopen(File_name,'r')
end
The code is in the 'dev' folder, I think my path is correct because when I do
open(File_name)
it opens.
Thanks for the help
If you want to open the file in the editor, use
open(File_name)
If you want to read data from the file, you can use
dlmread(File_name) % Read ASCII delimited file.
or
C = textscan(File,'FORMAT') % Read formatted data from text file or string.
or more low-level using fscanf, e.g., if the file contains three columns of integers you do the following: Read the values in column order, and transpose to match the appearance of the file: (from the help of fprintf)
fid = fopen('count.dat');
A = fscanf(fid,'%d',[3,inf])';
fclose(fid);
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');
I have multiple arrays with string data. All of them should be exported into a .csv file. The file should be saved in a subfolder. The file name is variable.
I used the code as follows:
fpath = ('./Subfolder/');
m_date = inputdlg('Date of measurement [yyyymmdd_exp]');
m_name = inputdlg('Characteristic name of the expteriment');
fformat = ('.csv');
fullstring = strcat(fpath, m_date,'_', m_name, fformat);
dlmwrite(fullstring,measurement);
However, I get an error that FILE must be a filename string or numeric FID
What's the reason?
Best
Andreas
What you are asking to do is fairly straightforward for Matlab or Octave. The first part is creating a file with a filename that changes. the best way to do this is by concatenating the strings to build the one you want.
You can use: fullstring = strcat('string1','string2')
Or specifically: filenameandpath = strcat('./Subfolder/FixedFileName_',fname)
note that because strings are pretty much just character arrays, you can also just use:
fullstring = ['string1','string2']
now, if you want to create CSV data, you'll first have to read in the file, possibly parse the data in some way, then save it. As Andy mentioned above you may just be able to use dlmwrite to create the output file. We'll need to see a sample of the string data to have an idea whether any more work would need to be done before dlmwrite could handle it.
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
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.