Input only numbers from .txt file and store numbers

1 回表示 (過去 30 日間)
Kamil akram
Kamil akram 2021 年 1 月 19 日
編集済み: dpb 2021 年 1 月 19 日
Hi, i am trying to import ONLY numbers from a text file and then storing those numbers in seperate integers
for eg. if i write the following statement in my txt file
(X-coordinate of first element i node = 1 Y-coordinate of first element i node = 2 X-coordinate of first element j node = 3 Y-coordinate of second element j node = 4 ...)
and i want to store them in matlab like this
x1i=1
y1i=2
x1j=3
y1j=4
i have tried eveything from fscanf, fgetl, fopen. Any help or in this regard would be helpful.
  1 件のコメント
Matt Gaidica
Matt Gaidica 2021 年 1 月 19 日
It's generally bad form to spawn new variables like that, it will be much easier to manage a matrix—is that possible for you? It sounds like you will need to do some text parsing to get the integers or a simple regex to find an integer after "=", but I would provide the actual text file for us to help.

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

回答 (1 件)

dpb
dpb 2021 年 1 月 19 日
編集済み: dpb 2021 年 1 月 19 日
It would be better to write a more parseable text file, at least with a delimiter if not multiple records...
With just highlevel MATLAB functions
s='your string';
a=split([s ' '],'=');
a=a(2:end);
a=extractAfter(a,' ')'
v=str2double(extractBefore(a,' '))
returns the array 1:4
Of course, regular expressions would be the other way to go.
And, as Matt says above, don't even think about poofing variables into the namespace..."there be dragons!"

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by