Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Find files with specific extension

96 views
Skip to first unread message

Nagmesh Kumar

unread,
Jul 12, 2012, 5:23:31 PM7/12/12
to
Hi,
I'm writing an algorithm where the user selects the directory & Matlab then looks for files in the directory that fall into a specific format.
The files are outputs of a logger and are named as: DATA-001.CSV, DATA-002.CSV....... Looking up help I found that >>dir *DATA*.CSV in the cmd prompt gives me all the files in that folder but I cannot save it as a variable >> x= dir *DATA*.csv
x= dir *DATA*.csv
|
Error: Unexpected MATLAB operator.

To sum up the objectives are:
Input: Path to the folder that has all the files
Output: Files are sequential='1' and Number of files=someNumber
Files are not Sequential='0' and disp('Files are not sequential. Please check the output data'
Logic: Look for files labelled as DATA-001.CSV until all the files are exhausted while checking for sequence (Sequence example: DATA-001.CSV, DATA-002.CSV.....). I'm planning to check for sequence using strcmp in a for loop.

If anything is unclear or if you have a faster or another way to solve this problem feel free to reply to this posting.
Thank you for your time.

Ben

unread,
Jul 12, 2012, 6:02:13 PM7/12/12
to
I've done something similar before, using regexp. Here are two lines that'll push you down the right path:
>> x = '*.txt';
>> dir(x)
license.txt patents.txt trademarks.txt

Matt Kindig

unread,
Jul 12, 2012, 6:19:13 PM7/12/12
to
I think this function will do what you want to do. I assume that you want sequential=false if there are any missing files. You can't assume that the files are in a particular order when dir() finds them, so we sort the numbers in the function.

function [sequential, numFiles] = ReadPath( Path)
files = dir( [Path '/DATA*.csv']); %get files matching pattern
files = { files.name}; %get only file names
expr = @(f) str2double( regexp(f, '(\d+)', 'match')); %regular expression
% assumes that only one set of digits appears in filename
nums = cellfun(expr, files, 'UniformOutput', true);
nums = sort(nums); %file numbers, sorted in order
checkorder = nums(1):1:nums(end); %comparison sequence that is in order
sequential = isequal( nums, checkorder);
if ~sequential,
disp('Files are not sequential. Please check the output data');
numFiles = 0;
else
numFiles = length(nums);
end
end

"Nagmesh Kumar" wrote in message <jtnf8j$b3$1...@newscl01ah.mathworks.com>...

Steven_Lord

unread,
Jul 13, 2012, 10:24:08 AM7/13/12
to


"Nagmesh Kumar" <nagmes...@gmail.com> wrote in message
news:jtnf8j$b3$1...@newscl01ah.mathworks.com...
> Hi,
> I'm writing an algorithm where the user selects the directory & Matlab
> then looks for files in the directory that fall into a specific format.
> The files are outputs of a logger and are named as: DATA-001.CSV,
> DATA-002.CSV....... Looking up help I found that >>dir *DATA*.CSV in the
> cmd prompt gives me all the files in that folder but I cannot save it as a
> variable >> x= dir *DATA*.csv
> x= dir *DATA*.csv
> |
> Error: Unexpected MATLAB operator.

You can't call a function both with an output argument and with command
form. If you want the function to return one or more output arguments, you
will need to use the function form.

x = dir('*DATA*.csv');

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Nagmesh Kumar

unread,
Jul 25, 2012, 2:19:18 PM7/25/12
to
Ben, Matt and Steve
Thank you very much for your help. It helped me compile the code by the deadline. This is the code I wrote. I'm just putting it out there so that others may use it.
Partial Code:
case 'Folder'
% User prompt to choose directory
directory= uigetdir('R:\WheelchairMonitoring',...
'Select the folder with the Gulf Coast Acclerometer data');
% get the right path for finding files
% with filename DATA and .csv extension
sdir=strcat(directory,'\*DATA*.CSV');
% get the files with DATA as filename & .csv as extension
% in the choosen folder
files=dir(sdir);
% Check for sequence in filename
for i=1:length(files)
if( str2double(files(i,1).name(6:8))==i)
% Gather filenames that are sequential
filename=fullfile(directory,files(i).name);
% Gather data from each file
dataFolder(i,1)=parseGCDCdata(filename);

else
disp('The files are not sequentially recorded check the folder');
end
end
% Now we have a bunch of structures that have
% each accelerometer data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Once again thank you for your support
Nagmesh
"Steven_Lord" <sl...@mathworks.com> wrote in message <jtpb28$fq$1...@newscl01ah.mathworks.com>...
0 new messages