MATLAB: How to read complex numbers from file and save them as type complex double

complex doublecomplex numbersfile read

I have generated a 1×1000 column file of complex numbers.
I want to read the file in matlab and save all the values as complex doubles for further caluclation
The text file is of the form:
(0.3215970586284238+0.6254311449388177j)
(0.3255286752579913+0.625780488857519j)
I have used this basic command to open and load the file
filename=uigetfile;
load(filename)
However I get this error message:
Error using load
Unknown text on line number 1 of ASCII file S11
"0.3215970586284238+0.6254311449388177j)
".

Best Answer

[filename, pathname] = uigetfile;
if isnumeric(filename); return; end %user canceled
filename = fullfile(pathname, filename);
[fid, message] = fopen(filename, 'rt');
if fid < 0; error('Failed to open file "%s" because "%s"', filename, message); end
data = cell2mat( textscan(fid, '(%f)') );
fclose(fid)
The textscan %f format already knows how to input complex numbers that use i or j .
Caution, though: the %f format can only detect complex numbers if there is no space between the real and imaginary part, which is the case for the data you show.