Clear Filters
Clear Filters

Input txt. file of complex numbers

7 views (last 30 days)
Sam Hurrell
Sam Hurrell on 29 Jun 2021
Commented: Johannes Hougaard on 30 Jun 2021
I have large data text files from COMSOL that contain multiple rows and columns of complex numbers. I have tried importing them into matlab as the files are quite large, but all the data is presented in a single column. eg:
-12.475000000000005 -1.975 2.5658824311275345E-6+4.421192310257837E-6i -4.688910807327458E-7-9.893058421633841E-8i 5.549150260222212E-6-2.1496631444278723E-5i -2.7187186095082164E-9+1.1189876165621303E-8i
I have tried to include Space as a column delimiter but it gives me the message: "com.mathworks.jmi.MatlabException: Arrays have incompatible sizes for this operation". How can I import this data properly?
  3 Comments
Sam Hurrell
Sam Hurrell on 29 Jun 2021
Edited: Sam Hurrell on 29 Jun 2021
I have inputed files from COMSOL to Matlab in the same way before and didn't have this problem. The only difference with this file is that it contained complex number
dpb
dpb on 29 Jun 2021
MAJOR weakness in the C formatted i/o library and incomprehensible to me that Mathworks hasn't addressed it in a scientific package -- there is no provision whatever for complex variables in the fscanf formatting -- and TMW hasn't done anything to help with all the new readXXX family, either.
And, the folks who wrote the file didn't do you any favors by including the real x,y coordinates on the same record as the first of the variables so will have to parse them out individually as well.
At least there is the header
% Dimension: 2
% Nodes: 20000
% Expressions: 60
that you can read to determine there are precisely two dimensions to read and then apparently "Expressions" is the number of values;
I didn't look at the whole file, but I then presume there are "Nodes" numbers of those repeated.
With some trial and tribulations, textscan or fscanf will be able to deal with this; or it might be more conducive albeit undoubtedly slower for a regular expressions conversion.
Might do a search and see if somebody has a FEX submission or there's a posted MATLAB function written already to parse a COMSOL file first, though...

Sign in to comment.

Answers (1)

Johannes Hougaard
Johannes Hougaard on 29 Jun 2021
As far as I can tell you can use the readmatrix function to read the file if you use the 'NumHeaderLines' option for the import.
COMSOL = readmatrix("COMSOL.TXT",'NumHeaderLines',9);
  5 Comments
Johannes Hougaard
Johannes Hougaard on 30 Jun 2021
What MATLAB version are you on? When I run the code I get:
>>comsol = readmatrix("COMSOL.txt",'NumHeaderLines',9);
>> whos('comsol')
Name Size Bytes Class Attributes
comsol 6x62 5952 double complex
>> comsol(:,3:6)
ans =
1.0e-04 *
0.0257 + 0.0442i -0.0047 - 0.0010i 0.0555 - 0.2150i -0.0000 + 0.0001i
0.0263 + 0.0453i -0.0049 - 0.0010i 0.0567 - 0.2206i -0.0000 + 0.0001i
0.0271 + 0.0465i -0.0051 - 0.0011i 0.0580 - 0.2265i -0.0000 + 0.0001i
0.0278 + 0.0477i -0.0053 - 0.0011i 0.0593 - 0.2326i -0.0000 + 0.0001i
0.0286 + 0.0489i -0.0055 - 0.0012i 0.0607 - 0.2390i -0.0000 + 0.0001i
0.0294 + 0.0502i -0.0057 - 0.0013i 0.0622 - 0.2456i -0.0000 + 0.0001i
Johannes Hougaard
Johannes Hougaard on 30 Jun 2021
...but if you'd prefer getting rid of readmatrix and using file level read options that's doable too
>> fid = fopen("COMSOL.txt",'r');
comsol = cell(0,0);
while ~feof(fid)
thisline = fgetl(fid);
comsol = vertcat(comsol,{thisline}); %#ok<AGROW> %accept that it's a growing variable although slightly slow
end
fclose(fid);
incl = false(size(comsol));
for ii = 1:length(comsol)
if ~strncmp(comsol{ii},'%',1)
comsol{ii} = str2num(comsol{ii}); %#ok<ST2NM> % str2num does the trick, str2double doesn't work on a formatted string
incl(ii) = true;
end
end
numericalcomsol = cell2mat(comsol(incl,:));
clear ans ii fid thisline incl
whos
Name Size Bytes Class Attributes
comsol 15x1 14272 cell
numericalcomsol 6x62 5952 double complex
>> numericalcomsol(:,3:6)
ans =
1.0e-04 *
0.0257 + 0.0442i -0.0047 - 0.0010i 0.0555 - 0.2150i -0.0000 + 0.0001i
0.0263 + 0.0453i -0.0049 - 0.0010i 0.0567 - 0.2206i -0.0000 + 0.0001i
0.0271 + 0.0465i -0.0051 - 0.0011i 0.0580 - 0.2265i -0.0000 + 0.0001i
0.0278 + 0.0477i -0.0053 - 0.0011i 0.0593 - 0.2326i -0.0000 + 0.0001i
0.0286 + 0.0489i -0.0055 - 0.0012i 0.0607 - 0.2390i -0.0000 + 0.0001i
0.0294 + 0.0502i -0.0057 - 0.0013i 0.0622 - 0.2456i -0.0000 + 0.0001i

Sign in to comment.

Categories

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

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!