Input complex matrix from txt file into matlab variable

10 views (last 30 days)
Hi! I have a txt file, where i have matrix about 30x1000. All numbers within are double and complex. So the question is - how to get this complex matrix into matlab to built plot? Cause when i try it some methods get Nan, some method get incorrect and not complex numbers.
Example of number in file : 1+1*i I can change it, and i tried different variant already. Always a i get nothing or smith about 49 when the number is just 1+ 1*i I tried ImportData, load and fopen, but maybe i tried something incorrect. Thank you for your help!
  1 Comment
per isakson
per isakson on 20 Mar 2015
"but maybe i tried something incorrect." &nbsp probably yes, but you supply to little detail for us to spot the error.

Sign in to comment.

Answers (2)

per isakson
per isakson on 20 Mar 2015
Edited: per isakson on 20 Mar 2015
textscan imports any complex number as a whole into a complex numeric field,
converting the real and imaginary parts to the specified numeric type (such
as %d or %f). [...]
Do not include embedded white space in a complex number. textscan interprets
embedded white space as a field delimiter.
Thus,
  • textscan should read your file without problems
  • 1+ 1*i with a space is not allowed.&nbsp
Warning: Test of reading 1-i with R2013b
str = '1+1i 1-i';
cac = textscan( str, '%f', 'CollectOutput', true );
cac{:}
ans =
1.0000 + 1.0000i
1.0000 + 0.0000i
No warning, no error just an unexpected result! I don't find this nasty behavior described in the documentation

Stephen23
Stephen23 on 20 Mar 2015
Edited: Stephen23 on 20 Mar 2015
You can use textscan to read a textfile with complex values. Here I show how this works on a string:
>> S = '5+6i 3-2i 1-1i @ 4+i 2-0.2i 2.5-1.3i @';
>> C = textscan(S,'%f%f%f@', 'CollectOutput',true);
>> C{1}
ans =
5 + 6i 3 - 2i 1 - 1i
4 + 1i 2 - 0.2i 2.5 - 1.3i

Community Treasure Hunt

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

Start Hunting!