MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

0

Seperate numbers in string to different array

Tyler Murray さんによって質問されました 2016 年 9 月 7 日
最新アクティビティ Tyler Murray さんによって コメントされました 2016 年 9 月 7 日
I import a text file using textscan and use vertcat to assign each line of the file its own cell. I am working with data to where it says something like: take off time 0.102 sec sensor1 initiated .361 sec, -31 (mv) sync at 50 ms and this continues for about 42 rows of data. My end goal is to separate all the numeric data from the words. For example have an array that would show [.102; .361 -31; 50;....]. I have tried regexp and sscanf but have had no luck.

  0 件のコメント

サインイン to comment.

タグ

2 件の回答

回答者: Azzi Abdelmalek
2016 年 9 月 7 日

str={'take off time 0.102 sec';'sensor1 initiated .361 sec, -31 (mv)';'sync at 50 ms'}
out=regexp(str,'\s[-\d\.]?\.?\d+','match')
out=str2double([out{:}])

  2 件のコメント

Tyler Murray 2016 年 9 月 7 日
Thanks for the answer Azzi, I had something similar to this but the problem is since it only gives the character number where it starts at, a number such as 0.12 will give indices of [1,3] (it doesn't count the decimal as part of the number). It also doesn't give insight to how large the number is. If I was to display character [1:3] in my example above it would display 0.1 instead of 0.12.
Azzi Abdelmalek
2016 年 9 月 7 日
I don't know if you have tested my code!

サインイン to comment.


回答者: Star Strider
2016 年 9 月 7 日

See if this does what you want:
str = 'take off time 0.102 sec sensor1 initiated .361 sec, -31 (mv) sync at 50 ms';
nbrvct = cellfun(@str2num, regexp(str, '\d+\.\d+|\.\d+|\-\d+|\d+', 'match'))
nbrvct =
0.102 1 0.361 -31 50

  1 件のコメント

Tyler Murray 2016 年 9 月 7 日
Thanks Star, this is similar to what I am looking for but got an error since each string is assigned to a cell. Error using str2num Requires string or character array input. I have it this way because I was trying to search for a phrase, figure out what cell it was in and display the number/numbers associated with that cell. Since the format can change a horizontal display may not work.

サインイン to comment.



Translated by