How can I read a complex number vector?
7 次查看(过去 30 天)
显示 更早的评论
Hi, I have this problem. I'm trying to read a complex number vector from a txt file using this structure:
fid=fopen ('VarName4.txt','r');
acorrente=fscanf(fid,'%f');
fclose (fid);
VarName4.txt is a vector 1000x1 like this:
[0.0141451080390089-0.00224036502008852i
0.0141451080390089-0.00224036502008852i
0.0141451080390089-0.00224036502008852i
0.0141451080390089-0.00224036502008852i...]
Using the structure before, the answer is:
acorrente =
0.0141
-0.0022
Why it doesn't work? Could someone teach me? Thanks a lot!
2 个评论
José-Luis
2014-2-11
It doesn't work because you are telling fscanf to expect a floating point number. Fscanf won't read in imaginary numbers.
采纳的回答
Mischa Kim
2014-2-11
编辑:Mischa Kim
2014-2-11
André Luiz, this should do:
fid = fopen ('VarName4.txt','r');
ii = 1;
while ~feof(fid)
acorrente(ii, :) = str2num(fgets(fid));
ii = ii + 1;
end
fclose(fid);
...read each line of the text file, convert it from string to number and save it in the matrix. The loop will keep reading until the end of the file ( feof ) is reached.
更多回答(1 个)
JasonLuo
2018-1-25
编辑:JasonLuo
2018-1-25
Use dlmread or textscan https://stackoverflow.com/questions/20538049/reading-complex-data-in-matlab-from-a-file
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!