How to read text file with mixed data types

16 views (last 30 days)
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

Accepted Answer

Jorge Rivé
Jorge Rivé on 22 Sep 2017
Edited: Jorge Rivé on 22 Sep 2017
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!