Convert array of comma separated strings in cells to matrix

24 ビュー (過去 30 日間)
Reid Elleman
Reid Elleman 2017 年 6 月 21 日
コメント済み: Jon 2022 年 7 月 15 日
Wordy, I know, but here's the short story.
I have a cell matrix like this:
Arr = {
'0,2,3'
'1,4,5'
'2,6,7'
};
that I need converted to numeric values, preferably in a matrix like this:
Mat = [
0 2 3;
1 4 5;
2 6 7;
];
The array is tens of thousands of lines long. I am currently using brute force to go line by line and convert each cell to a vector using strsplit and concatenating each row onto the bottom of Mat. Can anybody offer a better suggestion?
Here's the big picture if it helps:
I have a log file like this:
crap
crap
crap
HeaderCol1,HeaderCol2
0,4
1,5
2,6
3,7
crap
crap
HeaderCol1,HeaderCol2,HeaderCol3
0,1,2
1,2,3
2,3,4
3,4,5
crap
I need to import and plot the larger block of data and smaller block separately.
any help is appreciated and thank you.
  1 件のコメント
Jon
Jon 2022 年 7 月 15 日
Looks like you got some answers here that you liked. Would be good if you accepted one of them so that others, like me would get pointed to the one you would suggest using.

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

回答 (4 件)

Jan
Jan 2017 年 6 月 21 日
Arr = {
'0,2,3'
'1,4,5'
'2,6,7'};
Str = sprintf('%s,', Arr{:});
Num = sscanf(Str, '%g,', [3, inf]).';
  3 件のコメント
John Grant
John Grant 2019 年 8 月 3 日
I signed in just to thank you for this answer you gave, in delimiting comma spaced cell matrices. I've been struggling for hours, trying to breakup a cell matrix that had put all its data into 1 column, when it should have been 3 columns.
Janapati JayaLakshmi
Janapati JayaLakshmi 2020 年 3 月 12 日
works great, Really helped me. Thanks alot.

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


Walter Roberson
Walter Roberson 2017 年 6 月 22 日
Mat = cell2mat(cellfun(@str2num,Arr,'uniform',0));
  1 件のコメント
salman ashraf
salman ashraf 2021 年 4 月 2 日
編集済み: salman ashraf 2021 年 4 月 2 日
It worked well.
Thank you so much Walter Roberson. Your comment helped me a lot. Thank you

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


Andrei Bobrov
Andrei Bobrov 2017 年 6 月 21 日
Arr = {
'0,2,3'
'1,4,5'
'2,6,7'
};
T = regexprep(Arr,',','');
Brr = cell2mat(arrayfun(@(x)T{x}-'0',(1:numel(T))','un',0))

Ahmet Karakaya
Ahmet Karakaya 2020 年 1 月 4 日
1.Choose cell array on the workspace
2.Right click and copy contents
3. Paste between brackets
Array = [ ]
4.Array is a numeric array

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by