How do Iget a list from multiple rows?

2 ビュー (過去 30 日間)
Bernard Opoku
Bernard Opoku 2017 年 8 月 8 日
コメント済み: Bernard Opoku 2017 年 8 月 9 日
Hi guys, I have a multiple rows of strings and I want get words that contains "#" all in a single column.
Example:
data = {'he is #coming #today'; 'will #it rain?'};
The desired output:
out = {'#coming';
'#today';
'#it'}
Thanks
  1 件のコメント
Jan
Jan 2017 年 8 月 8 日
You forgot the quotes or double quotes. It matters if "strings" means cell strings or the modern string class. Please edit the question and post valid Matlab syntax.

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

採用された回答

Jan
Jan 2017 年 8 月 8 日
編集済み: Jan 2017 年 8 月 8 日
Perhaps these are cell strings:
data = {'he is #coming #today'; 'will #it rain?'};
words = strsplit(sprintf('%s ', data{:}), ' ').';
hasHash = ~cellfun('isempty', strfind(words, '#'));
result = words(hasHash);
There are many other ways. Does the # appear anywhere in the word? The example looks like it is the first character in all cases. Then:
data = {'he is #coming #today'; 'will #it rain?'};
words = strsplit(sprintf('%s ', data{:}), ' ').';
result = words(strncmp(words, '#', 1));
Or:
results = setdiff(words, strrep(words, '#', '*')).'
  1 件のコメント
Bernard Opoku
Bernard Opoku 2017 年 8 月 9 日
Thanks a lot Jan, that worked perfect!

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

その他の回答 (1 件)

Stephen23
Stephen23 2017 年 8 月 8 日
Using a regular expression is trivially easy:
>> data = {'he is #coming #today'; 'will #it rain?'};
>> C = regexpi(data,'#[a-z]+','match');
>> [C{:}]
ans =
'#coming' '#today' '#it'
  1 件のコメント
Bernard Opoku
Bernard Opoku 2017 年 8 月 9 日
many thanks Stephen, this is very easy to understand

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

カテゴリ

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