How to read text file with mixed data types

14 ビュー (過去 30 日間)
Jorge Rivé
Jorge Rivé 2017 年 9 月 22 日
編集済み: Jorge Rivé 2017 年 9 月 22 日
I've been searching everywhere for ways of doing this, and have tried multiple ways to read this data file in (textscan, dlmread,importdata, etc), but I can't figure how to handle the data types correctly. The data in the file looks like this:
2.13796208950223e-001 (2.38759502723354e-002dB,1.79999770619446e+002°)
I want to parse this into three columns, freq, magnitude (without the dB), and phase (without the degrees). Thanks for the help. Jorge

採用された回答

Jorge Rivé
Jorge Rivé 2017 年 9 月 22 日
編集済み: Jorge Rivé 2017 年 9 月 22 日
Ok...for anyone else struggling with something similar, I figured out a way. There are probably more elegant ways to do this, but at least I was able to get over this hump this way:
fileID=fopen(filename,'r');
delimiter='\t';
formatSpec='%f%s%[^\n\r]';
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false);
freq = dataArray{:, 1};
magNphase = dataArray{:, 2};
for i=1:length(magNphase)
splitmagNphase=strsplit(char(cellstr(magNphase{i})),',');
magtmp=erase(splitmagNphase{1},'(');
magtmp=erase(magtmp,'dB');
mag=[mag;str2num(magtmp)];
phtmp=erase(splitmagNphase{2},char(176));
phtmp=erase(phtmp,')');
ph=[ph;str2num(phtmp)];
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by