How to read text file with mixed data types

8 次查看(过去 30 天)
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 个)

Community Treasure Hunt

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

Start Hunting!

Translated by