How to converting complex numbers to absolute values?

30 visualizzazioni (ultimi 30 giorni)
Hello Matlab experts
I have difficulty with my complex data file. I would appreciate if some one can help me with it. The issue is how to import csv files containing complex values (as seen below) into matlab and then convert them into absolute values and also remove parantesis. I treid several commands but did not succeed it :(
This is how data look like. I also attach it as csv file:
theta\phi[rad] 0j (0.010489457941869092+0j) (0.020978915883738184+0j)(0.00393683289923533+0j) (-1.1834597816970487+0j) (-1.1825728101249893+0j) (-1.1815870828154462+0j)(0.00787366579847066+0j) (-2.498909050828868+0j) (-2.5891527984935+0j) (-2.5591423614638047+0j)(0.01181049869770599+0j) (-2.7942230095552336+0j) (-3.8767443114429523+0j) (-3.7833384009620286+0j)
Thank you very much
  2 Commenti
Stephen23
Stephen23 il 30 Mag 2022
"This is how data look like."
In reality it looks like this, including alternating lines with empty fields:
Whoever invented that file format is intentionally trying to make everyone else's lives harder. They are not a happy person.
Masoud Taleb
Masoud Taleb il 30 Mag 2022
Modificato: Masoud Taleb il 30 Mag 2022
Yes, it is indeed. It is created by a software for further analysis!

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 30 Mag 2022
Modificato: Stephen23 il 30 Mag 2022
No need for slow fiddling around with imported text via CELLFUN or evil EVAL.
Just specify the delimiter&whitespace and MATLAB will correctly import those complex numbers as numeric. Note that I modified the file (attached) by making a few of the imaginary numbers non-zero.
M = readmatrix('sample data.csv', 'Delimiter',',', 'Whitespace','()')
M =
NaN + 0.0000i 0.0000 + 0.0000i 0.0105 + 0.0000i 0.0210 + 0.0000i 0.0315 + 0.0000i 0.0420 + 0.0000i 0.0524 + 0.0000i 0.0000 + 1.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0039 + 0.0000i -1.1835 + 2.0000i -1.1826 + 0.0000i -1.1816 + 0.0000i -1.1805 + 0.0000i -1.1793 + 0.0000i -1.1780 + 0.0000i 0.0079 + 0.0000i -2.4989 + 0.0000i -2.5892 + 3.0000i -2.5591 + 0.0000i -2.5653 + 0.0000i -2.5740 + 0.0000i -2.5823 + 0.0000i 0.0118 + 0.0000i -2.7942 + 0.0000i -3.8767 + 0.0000i -3.7833 + 4.0000i -3.7168 + 0.0000i -3.6653 + 0.0000i -3.5654 + 0.0000i 0.0157 + 0.0000i -2.5550 + 0.0000i -4.6421 + 0.0000i -5.1153 + 0.0000i -5.1818 + 5.0000i -4.6118 + 0.0000i -4.6674 + 0.0000i
Then you can easily get the absolute value:
A = abs(M)
A = 6×7
NaN 0 0.0105 0.0210 0.0315 0.0420 0.0524 1.0000 0 0 0 0 0 0 0.0039 2.3239 1.1826 1.1816 1.1805 1.1793 1.1780 0.0079 2.4989 3.9628 2.5591 2.5653 2.5740 2.5823 0.0118 2.7942 3.8767 5.5058 3.7168 3.6653 3.5654 0.0157 2.5550 4.6421 5.1153 7.2008 4.6118 4.6674
Note that you could use READCELL, if that first element is important (but storing numeric data in a cell array like this makes processing the numeric data more difficult):
C = readcell('sample data.csv', 'Delimiter',',', 'Whitespace','()')
C = 6×7 cell array
{'theta\phi[rad]' } {[ 0]} {[ 0.0105]} {[ 0.0210]} {[ 0.0315]} {[ 0.0420]} {[ 0.0524]} {[0.0000 + 1.0000i]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0.0039]} {[-1.1835 + 2.0000i]} {[ -1.1826]} {[ -1.1816]} {[ -1.1805]} {[-1.1793]} {[-1.1780]} {[ 0.0079]} {[ -2.4989]} {[-2.5892 + 3.0000i]} {[ -2.5591]} {[ -2.5653]} {[-2.5740]} {[-2.5823]} {[ 0.0118]} {[ -2.7942]} {[ -3.8767]} {[-3.7833 + 4.0000i]} {[ -3.7168]} {[-3.6653]} {[-3.5654]} {[ 0.0157]} {[ -2.5550]} {[ -4.6421]} {[ -5.1153]} {[-5.1818 + 5.0000i]} {[-4.6118]} {[-4.6674]}
  1 Commento
Masoud Taleb
Masoud Taleb il 30 Mag 2022
Thnak you very much @Stephen23
This exactly does the work very fast. I really enjoyed how well written it is :)

Accedi per commentare.

Più risposte (1)

Chunru
Chunru il 30 Mag 2022
Modificato: Chunru il 30 Mag 2022
a = readcell('sample data.csv');
a{1} = 'NaN'
a = 6×7 cell array
{'NaN' } {[ 0]} {'(0.010489457941869092+0j)'} {'(0.020978915883738184+0j)'} {'(0.03146837382560728+0j)'} {'(0.04195783176747637+0j)'} {'(0.05244728970934546+0j)'} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {[ 0]} {'(0.00393683289923533+0j)'} {'(-1.1834597816970487+0j)'} {'(-1.1825728101249893+0j)' } {'(-1.1815870828154462+0j)' } {'(-1.1805027837737379+0j)'} {'(-1.1793201155129986+0j)'} {'(-1.1780392991030344+0j)'} {'(0.00787366579847066+0j)'} {'(-2.498909050828868+0j)' } {'(-2.5891527984935+0j)' } {'(-2.5591423614638047+0j)' } {'(-2.5652675338416144+0j)'} {'(-2.573976914138426+0j)' } {'(-2.5822604738413624+0j)'} {'(0.01181049869770599+0j)'} {'(-2.7942230095552336+0j)'} {'(-3.8767443114429523+0j)' } {'(-3.7833384009620286+0j)' } {'(-3.7167845156029586+0j)'} {'(-3.6652520844412733+0j)'} {'(-3.5654248689348518+0j)'} {'(0.01574733159694132+0j)'} {'(-2.554964246927875+0j)' } {'(-4.642096167506649+0j)' } {'(-5.1152675003827115+0j)' } {'(-5.1818108617674445+0j)'} {'(-4.611800660922098+0j)' } {'(-4.6674370489549935+0j)'}
b = zeros(size(a));
for i=1:numel(a)
if isnumeric(a{i})
b(i) = a{i};
else
%a{i}
b(i) = eval(a{i});
end
end
b
b = 6×7
NaN 0 0.0105 0.0210 0.0315 0.0420 0.0524 0 0 0 0 0 0 0 0.0039 -1.1835 -1.1826 -1.1816 -1.1805 -1.1793 -1.1780 0.0079 -2.4989 -2.5892 -2.5591 -2.5653 -2.5740 -2.5823 0.0118 -2.7942 -3.8767 -3.7833 -3.7168 -3.6653 -3.5654 0.0157 -2.5550 -4.6421 -5.1153 -5.1818 -4.6118 -4.6674

Categorie

Scopri di più su Variables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by