Split file name (alphanumeric name)
3 views (last 30 days)
Show older comments
I have a file names like: CAN20111230T134021UTC.mat
I need to split the file name and consider the files with month(12) i.e., 12.mat and run the loop based on that.
Could anyone please help me with this. Thanks in advance
0 Comments
Accepted Answer
Image Analyst
on 31 Mar 2015
The month is filename(8:9). You can extract that out of the full file name. However I don't know what "consider" means. You have files with names like CAN20111230T134021UTC.mat, but do you have or not have files with names like 12.mat? If you don't have them, then do you need to create them? Please explain exactly what "consider" means to you.
3 Comments
Image Analyst
on 31 Mar 2015
Extract the month from the filename and check if it's in the range 1-12.
month = str2double(filename(8:9));
if month >= 1 && month <= 12
% It's okay....
More Answers (1)
Stephen23
on 31 Mar 2015
Edited: Stephen23
on 1 Apr 2015
This is easy with my FEX submission datenum8601 that converts ISO 8601 date strings and converts them to date numbers, which can be converted to date vectors using datevec:
>> C = {'CAN20111230T134021UTC.mat';'CAN20111101T123456UTC.mat'};
>> V = datevec(cellfun(@datenum8601,C))
V =
2011 12 30 13 40 21
2011 11 1 12 34 56
The matrix of datevectors can be used to generate some logical indices for selecting only the desired filenames:
>> X = V(:,2)==12
X =
1
0
The logical indices can be used to select elements of a non-scalar structure, a cell array, or any other array.
Note that datenum8601 also returns the split parts of the strings (i.e. 'CAN' and 'UTC.mat'), and can be configured to recognize only particular date formats.
Filenames stored in a non-scalar structure can be converted to a cell array very simply, where name is the field of structure A containing the filenames:
C = {A.name};
2 Comments
Stephen23
on 1 Apr 2015
Edited: Stephen23
on 1 Apr 2015
You have already accepted another answer, which means that the question has been resolved. Accepting an answer tells other users that the problem has been resolved, so they will not bother to read this question. Is there still a problem?
I know that your data is in structures, that is exactly why I wrote the last sentences of my answer, to specifically address this topic. Did you read it, or look at the link? Do you understand the example that I gave?
So far you have not told us any details about this data structure: in particular you need to tell us if it is scalar or non-scalar, what the fieldnames are, and if they are nested with either cells or structs. Simply saying "my data file are structures" is not enough information for us.
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!