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!