How to import columns of complex numbers to Matlab variables from CSV file?

12 ビュー (過去 30 日間)
supun
supun 2017 年 3 月 3 日
コメント済み: Rahul Agnihotri 2021 年 9 月 14 日
I want to import the complex numbers in the attached file to matlab preferably as follows,
S11=the data in column B from 2nd row to 5th row and so on.
Thank you.

採用された回答

Walter Roberson
Walter Roberson 2017 年 3 月 4 日
Easiest way, R2013b or later:
datatable = readtable('VNA_S_Matrix_25mm.csv', 'Delimiter',',');
data = [datatable{:,1}, str2double(datatable{:,2:end})];
  2 件のコメント
supun
supun 2017 年 3 月 6 日
works great. I edited according to my need.
datatable = readtable('VNA_S_Matrix_25mm.csv', 'Delimiter',',');
S11 = transpose(str2double(datatable{:,2}));
S22 = transpose(str2double(datatable{:,7}));
Rahul Agnihotri
Rahul Agnihotri 2021 年 9 月 14 日
Works excatly like I wanted! Thanks for this.

サインインしてコメントする。

その他の回答 (1 件)

per isakson
per isakson 2017 年 3 月 4 日
編集済み: per isakson 2017 年 3 月 4 日
textscan (and readtable) reads complex numbers. Valid form for a complex number is: &nbsp ±<real>±<imag>i|j. Example: 5.7-3.1i.
The problem with your file is the spaces surrounding the sign of the imaginary part.
If the entire file fits in memory, this is a way to read it.
str = fileread( 'VNA_S_Matrix_25mm.csv' );
str = strrep( str, ' ', '' ); % remove spaces
cac = textscan( str, '%*f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f' ...
, 'Headerlines',1, 'Delimiter',',', 'CollectOutput',true );
and look at the result
>> cac
cac =
[4x16 double]
>> cac{1}(1:2)
ans =
0.1978 + 0.0323i -0.0270 - 0.1628i
>> cac{1}(4,15:16)
ans =
0.2387 - 0.0561i -0.3137 - 0.0229i
>>

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by