Value associate to a parameter in a text
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
So I have a text file similar to the following:
DELZZ 3301.23
KUTY 4.32 SERI -0.023
I want Matlab to read the file and return values associate to each parameter (DELZZ, KUTY,SERI,...) Any suggestion?
採用された回答
data = fileread('mytextfile.txt');
val = regexp(data,'(?<=\s)(-?\d*\.?\d*)(?=\s|$)','match');
7 件のコメント
Jack
2018 年 7 月 9 日
what does -?\d+\.?\d+ do?
- -? Matches a negative sign optionally.
- \d+ Matches digits.
- \. Matches a decimal point optionally.
- \d+ Matches more digits.
The output is:
val:
{'3301.23'} {'4.32'} {'-0.023'}
If you need to perform numerical calculations you need to convert the character vectors to doubles:
val = cellfun(@str2double,val);
Does that help you?
Thank you so much. And the last question, what if the name of parameters contained number, like
KUTY01 33.02
Fuzzy10 -0.923
or is it possible to ask Matlab: go ahead and find a value in front of a particular parameter? like find the value in front of "KUTY01" and it returns 33.02
Paolo
2018 年 7 月 9 日
If you wanted the result for a specific case, i.e. KUTY01, you can use:
(?<=KUTY01\s)-?\d+\.?\d+
The first part of the regular expression (?<=KUTY01\s) is a positive lookbehind.
As you can see here, it checks backwards to see if pattern KUTY01, followed by whitespace, is found. If it is, it matches the sequence of digits, decimal point and digits as explained in the previous comment. As you can see, only 33.02 is now matched.
If you found the answer useful consider accepting it/voting :)
Jack
2018 年 7 月 9 日
it seems this command doesn't read a single digit number like
KUTY01 5
is that right?
Paolo
2018 年 7 月 9 日
That's right, you will need to use the greedy * quantifier rather than the + quantifier. Since you need to match those values too, use:
(?<=KUTY01\s)-?\d*\.?\d*
I'll update my answer.
Jack
2018 年 7 月 9 日
Thank u so much
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
