How to search and extract text segment from a character array?

I have some text in a variable namely 'hdr'. From the workplace I see the type of the variable being a character array.
From the text stored in the variable, I want to extract the value of 'testdevice.x' and 'testdevice.y'. I tried strfind() but it says "TEXT must be a string or cell array of strings."
Thanks for helping
hlp.png

2 件のコメント

dpb
dpb 2018 年 11 月 20 日
Convert to either cellstr() or string() first, then regexp is vectorized to find more general patterns or use the new(ish) string...
hdr=string(hdr); % convert to string
ix=contains(hdr,"device.x")| contains(hdr,"device.y"); % find device.x|y
device=hdr(ix); % return those lines
then can parse those lines for the data
Md Nur Kutubul Alam
Md Nur Kutubul Alam 2018 年 11 月 21 日
It says "Undefined function 'string' for input arguments of type 'char'."

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

回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2018 年 11 月 21 日
編集済み: Andrei Bobrov 2018 年 11 月 21 日

0 投票

c = cellstr(hdr);
out = str2double(...
regexp(c,'(?<=(testdevice\.x|testdevice\.у)\s*\:\s*)\-?\d*','match','once'));
out = out(~isnan(out));

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2018 年 11 月 20 日

編集済み:

2018 年 11 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by