|
|
Find files with specific extension #2
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.
|
|
0
|
|
|
|
Reply
|
nagmeshkumar (3)
|
7/12/2012 9:23:31 PM |
|
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
|
|
0
|
|
|
|
Reply
|
ben-walker (128)
|
7/12/2012 10:02:13 PM
|
|
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>...
> 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.
|
|
0
|
|
|
|
Reply
|
mkindig (11)
|
7/12/2012 10:19:13 PM
|
|
"Nagmesh Kumar" <nagmeshkumar@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
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
|
|
0
|
|
|
|
Reply
|
slord (13285)
|
7/13/2012 2:24:08 PM
|
|
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" <slord@mathworks.com> wrote in message <jtpb28$fq$1@newscl01ah.mathworks.com>...
>
>
> "Nagmesh Kumar" <nagmeshkumar@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
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
|
|
0
|
|
|
|
Reply
|
nagmeshkumar (3)
|
7/25/2012 6:19:18 PM
|
|
|
4 Replies
25 Views
(page loaded in 0.101 seconds)
|
|
|
|
|
|
|
|
|