I need help converting cells of complexes into real/imaginary matrices

4 views (last 30 days)
Dear Colleagues,
I'm trying to real admittance matrix from csv/excel form into matlab workspace.
I was able to get
ddd =
{["0.00 - j17.36" ]} {["" ]} {["" ]} {["-0.00 + j17.36"]} {["" ]} {["" ]}
{["" ]} {["0.00 - j16.00" ]} {["" ]} {["" ]} {["" ]} {["" ]}
{["" ]} {["" ]} {["0.00 - j17.06" ]} {["" ]} {["" ]} {["" ]}
{["-0.00 + j17.36"]} {["" ]} {["" ]} {["4.06 - j42.10" ]} {["-2.12 + j14.39"]} {["-1.94 + j10.51"]}
{["" ]} {["" ]} {["" ]} {["-2.12 + j14.39"]} {["3.30 - j20.13" ]} {["" ]}
{["" ]} {["" ]} {["" ]} {["-1.94 + j10.51"]} {["" ]} {["3.17 - j15.73" ]}
{["" ]} {["-0.00 + j16.00"]} {["" ]} {["" ]} {["-1.19 + j5.98" ]} {["" ]}
{["" ]} {["" ]} {["" ]} {["" ]} {["" ]} {["" ]}
{["" ]} {["" ]} {["-0.00 + j17.06"]} {["" ]} {["" ]} {["-1.23 + j5.48" ]}
I'm trying to change this cell array into matrices of real and imaginary numbers.
But cell2mat or str2double doesn't work with my data.
Is there anyone who already faced this issue?
Thank you in advance.

Accepted Answer

Walter Roberson
Walter Roberson on 24 Dec 2020
str2double( regexprep(cellstr(ddd), 'j(.*)', '$1i', 'once'))
If you want the empty cells to be converted to 0 then
str2double(regexprep(cellstr(ddd), {'j(.*)','^$'}, {'$1i', '0'}, 'once', 'emptymatch'))

More Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 24 Dec 2020
It seem the issue with data format, please have a look in the following example
data={1+1i,3+6i} % Sample data
data =
1×2 cell array
{[1.0000 + 1.0000i]} {[3.0000 + 6.0000i]}
result=cell2mat(data) %Cell 2 mat
result =
1.0000 + 1.0000i 3.0000 + 6.0000i
real_mat=real(result) %Real part of result mat
real_mat =
1 3
imag_mat=imag(result) %Imag part of result mat
imag_mat =
1 6

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!