Split file name (alphanumeric name)

조회 수: 5 (최근 30일)
anu
anu 2015년 3월 31일
댓글: anu 2015년 4월 14일
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

채택된 답변

Image Analyst
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
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....
anu
anu 2015년 4월 14일
thanks for your help. It worked.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
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};
If the names are contained in all fields of a scalar structure, then structfun could be used instead of cellfun.
  댓글 수: 2
anu
anu 2015년 4월 1일
Thanks for your help. But my data file are structures not cell arrays. So, how can I implement on structures.
Stephen23
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 CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by