How to convert string to matrix

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

 Accepted Answer

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 Comments

Thank you
My pleasure!
ADDENDUM —
To accommodate strings such as ‘A’ with different numbers of elements between the ‘@’ signs, something like this works:
A='5 1 2 2 3@4 3 3 4 5@4 7 8 7 9@4 44 3 3 12@';
A='5 5 3@ 4 5 5@ 4 7 89@ 3 8 12@';
at = strfind(A,'@');
ats = length(at);
atn = strsplit(A,'@');
a = strsplit(atn{1});
satn = length(a);
B = strrep(A,'@',' ');
C = char(strsplit(B));
D = reshape(str2num(C), satn, [])';
I’m certain this can be made more efficient. It has the virtue of being reasonably robust (that is, it works with every ‘A’ you’ve presented) thus far.
As Image Analyst mentions, your strings have to be compatible with the constraints of the reshape function, or my code will not work.
I will leave it to others to make this more efficient, but you’ve accepted my Answer, so I will give you my best effort.
Thank you for your effort and your time..I really appreciate your help...
I've tried your code with
A='19 149 @ 19 148 @ 20 147 @ 21 146 @ 21 145 @ ... and so on ..it has 1324 elements
but I got error
Error using reshape Product of known dimensions, 3, not divisible into total number of elements, 1324.
Or in some cases it works but it displays wrong result
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 on 21 Dec 2014
Edited: Lolipop on 21 Dec 2014
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

Sign in to comment.

More Answers (2)

A='55 3@ 4 5@ 47 89@ 33 12@'
out = reshape(str2double(regexp(A,'\d*','match')),2,[])'
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 Comments

All these codes working for this example
A='55 3@ 4 5@ 47 89@ 33 12@'
But in my text file I have some matrixes that are
A='5 5 3@ 4 5 5@ 4 7 89@ 3 8 12@'
or like
A='5 1 2 2 3@4 3 3 4 5@4 7 8 7 9@4 44 3 3 12@'
or with 6 or 7 columns.
Is there some universal code that is going to work for all examples above?
Well, you should have said that in the beginning - no one planned for that.
In that case, the best answer is an adaptions of Andrei's:
clc;
A='55 3@ 4 5@ 47 89@ 33 12@' % Create sample data.
atLocation = find(A=='@');
numNumbers = 1+sum(A(1:atLocation)==' ')
out = reshape(str2double(regexp(A,'\d*','match')), numNumbers,[])'
A='5 5 3@ 4 5 5@ 4 7 89@ 3 8 12@'
atLocation = find(A=='@');
numNumbers = 1+sum(A(1:atLocation)==' ')
out = reshape(str2double(regexp(A,'\d*','match')), numNumbers,[])'
A='5 1 2 2 3@4 3 3 4 5@4 7 8 7 9@4 44 3 3 12@'
atLocation = find(A=='@');
numNumbers = 1+sum(A(1:atLocation)==' ')
out = reshape(str2double(regexp(A,'\d*','match')), numNumbers,[])'
If this helps, please "Vote" for my and Andrei's answers.
I did. We’ll have to wait for ‘delila’ to return.
I should have told it on beginning but I have matrixes in file with various format... and I thought that code would work for all of them or that I would know to change to work for all of them...
Your code is also working for examples above but on string with spaces doesn't
A='79 197 @ 80 197 @ 81 198 @ 82 198 @ 83 199 @';
Thank you for your effort
There is a function called strtrim() that you should know about. You can use it:
A='79 197 @ 80 197 @ 81 198 @ 82 198 @ 83 199 @';
atLocation = find(A=='@');
trimmedString = strtrim(A(1:atLocation-1))
numNumbers = 1+sum(trimmedString==' ')
out = reshape(str2double(regexp(A,'\d*','match')), numNumbers,[])'
This works for all 3 cases that you've specified so far. Now, what are the remaining cases? We'd rather not modify the code case by case until it works for all cases. We'd like to know all possible cases in advance . It will save both you and us time.
Lolipop
Lolipop on 21 Dec 2014
Edited: Lolipop on 21 Dec 2014
I am quite new with Matlab and still I don't know all functions..so I didn't know that function strtrim even exist... Thank you a lot..and there is no more cases :)
when I input the value of A with an edit text.. why uitable didn't show anything ?
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.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!