How to read text file with mixed data types

13 vues (au cours des 30 derniers jours)
Jorge Rivé
Jorge Rivé le 22 Sep 2017
Modifié(e) : Jorge Rivé le 22 Sep 2017
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

Réponse acceptée

Jorge Rivé
Jorge Rivé le 22 Sep 2017
Modifié(e) : Jorge Rivé le 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

Plus de réponses (0)

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by