dlmread with Complex Data

9 views (last 30 days)
Andrew
Andrew on 25 Jul 2012
Hello,
I am trying to load the csv file which is linked to at the end of the post into MATLAB. The file contains 11 lines and 2 columns. The data in the second column is complex. There is no header.
The following code throws an exception
folder = 'C:\Users\...\';
M= csvread([folder '\sweepRect1_4.csv'])
The exception is:
Error using dlmread (line 139)
Mismatch between file and format string.
Trouble reading number from file (row 2u, field 1u) ==>
0.0694068104667148i\n
Error in csvread (line 48)
m=dlmread(filename, ',', r, c);
From reading the documentation on csvread, my understanding is that the file is properly formatted. I have looked at the csv file in notepad and notepad++ and I do not see any extra characters that would be 'non numeric'. My understanding is that the \n is necessary and proper in a csv file. Note, I get this error with csv-files generated by Ansoft's HFSS (on Windows) and never edited by MS Excel. This file was cropped using MS Excel, but the exception is the same.
So, how do I load this file? I need to load several large files, and hand editing the files would be a last resort.
Thank you very much for your time,
Andrew
https://skydrive.live.com/redir?resid=C643CB32E791B631!618&authkey=!AG-2C2QrnBMKWD8
MATLAB Version: 7.14.0.739 OS: Windows 7

Accepted Answer

per isakson
per isakson on 25 Jul 2012
Edited: per isakson on 25 Jul 2012
str = '0, -0.327608566493008 + 0.0694068104667148i';
cac = textscan( str, '%u%f', 'delimiter', ',', 'whitespace', ' ' )
This doesn't work. The problem is the spaces between the real and imaginary parts.
str = '0, -0.327608566493008+0.0694068104667148i';
cac = textscan( str, '%u%f', 'delimiter', ',', 'whitespace', ' ' )
This works. Without these spaces dmlread would probably work aswell. It is documented, I cannot blame Matlab this time :)
Try
  1. read the complete line as a string
  2. remove spaces next to "+" and "-"
  3. convert according to above example

More Answers (1)

Andrew
Andrew on 25 Jul 2012
Thank you again, perisakson.
And thank goodness for find and replace.

Community Treasure Hunt

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

Start Hunting!