MATLAB: How to convert string with complex numbers to matrix

complexcomplex numbersmatrixstringtext filetextscan

Hi everyone!
I have read matrix from the text file and got string like this:
A= 5+6i 3-2i 1-i @ 4+i 2-0.2i 2.5-1.3i @
where @ means it's new row
So I need to write it like this:
A =
5+6i 3-2i 1-i
4+i 2-0.2i 2.5-1.3i
I use this code:
atLocation = find(A=='@');
trimmedString = strtrim(A(1:atLocation-1))
numNumbers = 1+sum(trimmedString==' ')
out = reshape(str2double(regexp(A,'\d*','match')), numNumbers,[])'
But this is working only for numbers. So, when I try this code with complex number it is not working.
If anyone knows where is the problem help me please.

Best Answer

E.g. simple code assuming no blanks in the numbers,
S = '5+6i 3-2i 1-i @ 4+i 2-0.2i 2.5-1.3i @'
S(S=='@') = ';';
x = find(S=='=',1);
if( isempty(x) )
x = 0;
end
eval([S(1:x) '[' S(x+1:end) ']'])
ans =
5.0000 + 6.0000i 3.0000 - 2.0000i 1.0000 - 1.0000i
4.0000 + 1.0000i 2.0000 - 0.2000i 2.5000 - 1.3000i