find function using wildcards

21 ビュー (過去 30 日間)
sermet OGUTCU
sermet OGUTCU 2021 年 9 月 17 日
コメント済み: Rik 2021 年 9 月 17 日
fileID = fopen(full_file_name);
header = textscan(fileID, '%[^,\n]');
fclose(fileID);
end_of_header_line=find(contains(header{1,1},'* '));
Here, I find all line numbers of header includes any * with two spaces or more. I need to modify the last part of the above codes to find all line numbers includes * with two spaces and following four digit numbers such as;
* 2021
For example header includes:
/* GeoForschungsZentrum Potsdam
/*
/*
* 2021 3 28 0 0 0.00000000
The line numbers (end_of_header_line) produced from above codes includes all 4 lines. What I need to find only the last line.

採用された回答

Rik
Rik 2021 年 9 月 17 日
You can use the patterns introduced in R2020b:
str={'/* GeoForschungsZentrum Potsdam '
'/* '
'/* '
'* 2021 3 28 0 0 0.00000000 '};
pat="*"+whitespacePattern(2,inf)+digitsPattern(1);
contains(str,pat)
ans = 4×1 logical array
0 0 0 1
Alternatively you can use regular expressions, which are not much harder to use.
  2 件のコメント
sermet OGUTCU
sermet OGUTCU 2021 年 9 月 17 日
Dear @Rik, my matlab version is R2019a. If it is possible, could you show me the another way that will be worked in this version?
Rik
Rik 2021 年 9 月 17 日
When you posted your question you had the opportunity to enter your release. Please do so next time. As you can see it is sometimes relevant.
str={'/* GeoForschungsZentrum Potsdam '
'/* '
'/* '
'* 2021 3 28 0 0 0.00000000 '};
pat=['\*',... % a literal *
'\s\s','\s*',...% two whitespace characters, followed by 0 or more
'\d']; % any digit
pos=regexp(str,pat) %regexp returns the position of a match
pos = 4×1 cell array
{0×0 double} {0×0 double} {0×0 double} {[ 1]}
%The legacy syntax of cellfun is the only case where it is faster than a
%loop. However, it doesn't work properly with all data types.
logical(cellfun('prodofsize',pos))
ans = 4×1 logical array
0 0 0 1

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by