Read .csv file with whitespace in complex number using textscan
이전 댓글 표시
I've got several .csv files with complex numbers delimited by commas. Example:
-0.0614049146709751 - 0.967215853912349i,-0.181447681051604 - 0.950981306143405i
Unfortunately the complex numbers in the files have whitespaces between the real and imaginary parts. This causes trouble when using textscan with format specifier %f%f. It returns the following error (when reading a real file with more inputs than 2 complex numbers and a format specifier of %f matching the number of inputs):
Error using textscan
Mismatch between file and format string.
Trouble reading 'Numeric' field from file (row number 1, field number 3) ==> - 0.967215853912349i,-0.181447681051604 -
0.950981306143405i,-0.290439723735331 - 0.922256442301523i,-0.409254287788005 - 0.874527909680631i,-0.541371606889636 -
0.797029055539859i,-0.694965707267823 ...
I've come up with the following solutions:
- Manually search and replace ' + ' and ' - ' in each file outside matlab.
- Read the data as strings, remove the whitespaces and convert to complex numbers in matlab.
Is there a better way to make textscan ignore those whitespaces?
Thanks in advance!
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!