process a list with specific name and extension in matlab - matlab

I'm trying to process a list of files that start with the same string, but only the .mat files. In my folder I have log files with names such as:
CADS3P5Ph1_LKS_20141210_EVAL_103443_001.avi
CADS3P5Ph1_LKS_20141210_EVAL_103443_001_MeasData.mat
CADS3P5Ph1_LKS_20141210_EVAL_103443_002.avi
CADS3P5Ph1_LKS_20141210_EVAL_103443_002_MeasData.mat
CADS3P5Ph1_LKS_20141210_EVAL_103443_003.avi
CADS3P5Ph1_LKS_20141210_EVAL_103443_003_MeasData.mat
CADS3P5Ph1_LKS_20141210_EVAL_104236_001.avi
CADS3P5Ph1_LKS_20141210_EVAL_104236_001_MeasData.mat
I only need to process the files that have the same timestamp (e.g. 103443_xxx)
I made a variable looking with a wildcard
filename = CADS3P5Ph1_LKS_20141210_EVAL_103443_001_MeasData.mat
general_name = filename(1:end - 17);
general_name = strcat(general_name,'*','');
So when I do dir(general_name), it finds all the files that start with "CADS3P5Ph1_LKS_20141210_EVAL_103443",
How do I only get the .mat files, and not the .avi files
I tried
dir(general_name && *.mat)
Is there a way to make something like this work?
Thanks!

Using strcat with general_name and the wildcard character for .mat extensions should work:
dir(strcat(general_name,'*.mat'))

Related

read multiple file from folder

I want to read multiple files from a folder but this code does not work properly:
direction=dir('data');
for i=3:length(direction)
Fold_name=strcat('data\',direction(i).name);
filename = fullfile(Fold_name);
fileid= fopen(filename);
data = fread (fileid)';
end
I modified your algorithm to make it easier
Just use this form :
folder='address\datafolder\' ( provide your folder address where data is located)
then:
filenames=dir([folder,'*.txt']); ( whatever your data format is, you can specify it in case you have other files you do not want to import, in this example, i used .txt format files)
for k = 1 : numel(filenames)
Do your code
end
It should work. It's a much more efficient method, as it can apply to any folder without you worrying about names, number order etc... Unless you want to specify certain files with the same format within the folder. I would recommend you to use a separate folder to put your files in.
In case of getting access to all the files after reading:
direction=dir('data');
for i=3:length(direction)
Fold_name=strcat('data\',direction(i).name);
filename = fullfile(Fold_name);
fileid(i)= fopen(filename);
data{i-2} = fread (fileid(i))';
end

How to remove a pattern from filename in Matlab

I'd like to remove '-2' from the filenames looking like this:
EID-NFBSS-2FE454B7-2_TD.eeg
EID-NFBSS-2FE454B7-2_TD.vhdr
EID-NFBSS-2FE454B7-2_TD.vmrk
EID-NFBSS-3B3BF9FA-2_BU.eeg
EID-NFBSS-2FE454B7-2_PO.txt
So as you may see the names of the files are different and there are different kind of extensions as well. All what I want to do is remove '-2' from all of the filenames. I was trying use this:
pattern = '-2';
replacement = '';
regexprep(filename,pattern,replacement)
and I got the results in the console, but after many attempts I have no idea how to 'say' to MATLAB switch the filnames in the same location.
#excaza hit it right on the money. You'll have to probe your desired directory for a list of files via dir, then loop through each filename and remove any occurrences of -2, then use movefile to rename the file, and delete to delete the old file.
Something like this comes to mind:
%// Get all files in this directory
d = fullfile('path', 'to', 'folder', 'here');
directory = dir(d);
%// For each file in this directory...
for ii = 1 : numel(directory)
%// Get the relative filename
name = directory(ii).name;
%// Replace any instances of -2 with nothing
name_after = regexprep(name, '-2', '');
%// If the string has changed after this...
if ~strcmpi(name, name_after)
%// Get the absolute path to both the original file and
%// the new file name
fullname = fullfile(directory, name);
fullname_after = fullfile(directory, name_after);
%// Create the new file
movefile(fullname, fullname_after);
%// Delete the old file
delete(fullname);
end
end
The logic behind this is quite simple. First, the string d determines the directory where you want to search for files. fullfile is used to construct your path by parts. The reason why this is important is because this allows the code to be platform agnostic. The delineation to separate between directories is different between operating systems. For example, in Windows the character is \ while on Mac OS and Linux, it's /. I don't know which platform you're running so fullfile is going to be useful here. Simply take each part of your directory and put them in as separate strings into fullfile.
Now, use dir to find all files in this directory of your choice. Replace the /path/to/folder/here with your desired path. Next, we iterate over all of the files. For each file, we get the relative filename. dir contains information about each file, and the field you want that is most important is the name attribute. However, this attribute is relative, which means that only the filename itself, without the full path to where this file is stored is given. After, we use regexprep as you have already done to replace any instances of -2 with nothing.
The next point is important. After we try and change the filename, if the file isn't the same, we need to create a new file by simply copying the old file to a new file of the changed name and we delete the old file. The function fullfile here helps establish absolute paths to where your file is located in the off-chance that are you running this script in a directory that doesn't include the files you're looking for.
We use fullfile to find the absolute paths to both the old and new file, use movefile to create the new file and delete to delete the old file.

Saving strings from the input .txt filename - MATLAB

Via textscan, I am reading a number of .txt files:
fid1 = fopen('Ev_An_OM2_l5_5000.txt','r');
This is a simplification as in reality I am loading several hundred .txt files via:
files = dir('Ev_An*.txt');
Important information not present within the .txt files themselves are instead part of the filename.
Is there a way to concisely extract portions of the filename and save them as strings/numbers? For example saving 'OM2' and '5000' from the above filename as variables.
fileparts appears to require the full path of the file rather than just defaulting to the MATLAB folder as with textscan.
It depends on how fixed your filename is. If your filename is in the string filename, then you can use regexp to extract parts of your filename, like so:
filename = 'Ev_An_OM2_l5_5000.txt'; %or whatever
parts = regexp(filename,'[^_]+_[^_]+_([^_]+)_[^_]+_([^\.]+)\.txt','tokens');
This will give you parts{1}=='OM2' and parts{2}=='5000', assuming that your filename is always in the form of
something_something_somethingofinterest_something_somethingofinterest.txt
Update:
If you like structs more than cells, then you can name your tokens like so:
parts = regexp(filename,'[^_]+_[^_]+_(?<first>[^_]+)_[^_]+_(?<second>[^\.]+)\.txt','names');
in which case parts.first=='OM2' and parts.second=='5000'. You can obviously name your tokens according to their actual meaning, since they are important. You just have to change first and second accordingly in the code above.
Update2:
If you use dir to get your filenames, you should have a struct array with loads of unnecessary information. If you really just need the file names, I'd use a for loop like so:
files = dir('Ev_An*.txt');
for i=1:length(files)
filename=files(i).name;
parts = regexp(filename,'[^_]+_[^_]+_(?<first>[^_]+)_[^_]+_(?<second>[^\.]+)\.txt','tokens');
%here do what you want with parts.first, parts.second and the file itself
end

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

importing excel into matlab

I have 4 folders in the same directory where each folder contains ~19 .xls files. I have written the code below to obtain the name of each of the folders and the name of each .xls file within the folders.
path='E:\Practice';
folder = path;
dirListing = dir(folder);
dirListing=dirListing(3:end);%first 2 are just pointers
for i=1:length(dirListing);
f{i} = fullfile(path, dirListing(i,1).name);%obtain the name of each folder
files{i}=dir(fullfile(f{i},'*.xls'));%find the .xls files
for j=1:length(files{1,i});
File_Name{1,i}{j,1}=files{1,i}(j,1).name;%find the name of each .xls file
end
end
Now I'm trying to import the data from excel into matlab by using xlsread. What I'm struggling with is knowing how to load the data into matlab within a loop where the excel files are in different directories (different folders).
This leaves me with a 1x4 cell named File_Name where each cell refers to a different folder located under 'path', and within each cell is then the name of the spreadsheets wanting to be imported. The size of the cells vary as the number of spreadsheets in each folder varies.
Any ideas?
thanks in advance
I'm not sure if I'm understanding your problem, but all you have to do is concatenate the strings that contain directory (f{}) and the file name. Modifying your code:
for i=1:length(dirListing);
f{i} = fullfile(path, dirListing(i,1).name);%obtain the name of each folder
files{i}=dir(fullfile(f{i},'*.xls'));%find the .xls files
for j=1:length(files{1,i});
File_Name{1,i}{j,1}=files{1,i}(j,1).name;%find the name of each .xls file
fullpath = [f{i} '/' File_Name{1,i}{j,1}];
disp(['Reading file: ' fullpath])
x = xlsread(fullpath);
end
end
This works on *nix systems. You may have to join the filenames with a '\' on Windows. I'll find a more elegant way and update this posting.
Edit: The command filesep gives the forward or backward slash, depending on your system. The following should give you the full path:
fullpath = [f{i} filesep File_Name{1,i}{j,1}];
Take a look at this helper function, written by a member of the matlab community.
It allows you to recursively search through directories to find files that match a certain pattern. This is a super handy function to use when looking to match files.
You should be able to find all your files in a single call to this function. Then you can loop through the results of the rdir function, loading the files one at a time into whatever data structure you want.