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 CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by