![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1015595/image.png)
How to converting complex numbers to absolute values?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Masoud Taleb
le 29 Mai 2022
Modifié(e) : Masoud Taleb
le 30 Mai 2022
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 commentaires
Stephen23
le 30 Mai 2022
"This is how data look like."
In reality it looks like this, including alternating lines with empty fields:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1015595/image.png)
Whoever invented that file format is intentionally trying to make everyone else's lives harder. They are not a happy person.
Réponse acceptée
Stephen23
le 30 Mai 2022
Modifié(e) : Stephen23
le 30 Mai 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','()')
Then you can easily get the absolute value:
A = abs(M)
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','()')
Voir également
Catégories
En savoir plus sur Data Import and Analysis dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!