Split file name (alphanumeric name)
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2015-3-31
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 个评论
Image Analyst
2015-3-31
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....
更多回答(1 个)
Stephen23
2015-3-31
编辑:Stephen23
2015-4-1
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 个评论
Stephen23
2015-4-1
编辑:Stephen23
2015-4-1
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!