How to convert string to matrix

61 ビュー (過去 30 日間)
Lolipop
Lolipop 2014 年 12 月 20 日
コメント済み: Image Analyst 2015 年 10 月 30 日
Hi!
From text file I have read matrix and I got string
A=55 3@ 4 5@ 47 89@ 33 12@
where '@' means it's new row. How will I make matrix from this string to look like this
A =
55 3
4 5
47 89
33 12
so A is size 4x2 class double
Please if anyone knows the answer help me

採用された回答

Star Strider
Star Strider 2014 年 12 月 20 日
This works:
A='55 3@ 4 5@ 47 89@ 33 12@';
B = strrep(A,'@',' ');
C = char(strsplit(B));
D = reshape(str2num(C), 2, [])';
‘D’ is the output.
  6 件のコメント
Star Strider
Star Strider 2014 年 12 月 21 日
My pleasure!
As Image Analyst mentioned, your strings have to be compatible with the size constraints the reshape function imposes. If your strings were created from matrices that are compatible with reshape, and conform to the format of the strings you posted, there should be no problems.
All I can claim is that it works with the ‘A’ strings you provided. Your strings must conform to the format of the strings you posted for my code to work with them. It assumes those are representative of all your strings.
You may have to do some creative coding to account for the other strings you have, such as adding or deleting consecutive ‘@’ signs (guessing here), and perhaps adding NaN values at the end — even manually — to make it compatible with reshape. I doubt any code would ever be robust to all inputs to it.
I encourage you to see the documentation for the reshape function to understand its requirements.
Lolipop
Lolipop 2014 年 12 月 21 日
編集済み: Lolipop 2014 年 12 月 21 日
I understood where's problem I tried with matrix where have spaces with @ like this
A='79 197 @ 80 197 @ 81 198 @ 82 198 @ 83 199 @'
and it doesn't work but I don't know how to fix it
I thought that spaces between @ were irrelevant

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

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2014 年 12 月 20 日
A='55 3@ 4 5@ 47 89@ 33 12@'
out = reshape(str2double(regexp(A,'\d*','match')),2,[])'
  1 件のコメント
Lolipop
Lolipop 2014 年 12 月 20 日
Thank you this is working too :D

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


Image Analyst
Image Analyst 2014 年 12 月 20 日
Here's yet another way:
A='55 3@ 4 5@ 47 89@ 33 12@' % Create sample data.
intA = sscanf(A, '%d %d@') % Make string into numerical array.
s=reshape(intA, [numel(intA)/2, 2]) % Shape into two column matrix.
Note, none of the methods given are that robust in that they don't check to see that A has an even number of integers. For robust code, you need to check for that. It's not bulletproof code unless you are prepared to handle things like that - inputs like A='55 3@ 4 5@ 47 89@ 33@' for example.
  8 件のコメント
doni yandra
doni yandra 2015 年 10 月 30 日
when I input the value of A with an edit text.. why uitable didn't show anything ?
Image Analyst
Image Analyst 2015 年 10 月 30 日
Only Walter has the Crystal Ball Toolbox. The rest of us will need to see your code in a more direct manner, like pasted back here or to a brand new question.

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by