Import alphanumeric data from .txt

조회 수: 1 (최근 30일)
Umberto
Umberto 2012년 10월 22일
Hi,
I'm searching to extract datas from a .txt.
The txt. is an instrument output that give me something like this:
02062012025000 0R0,Hp=0.0M,Th=9.8C,Vh=0.0N,Vs=13.4V,Vr=3.484V,Id=Hel
02062012025100 0R0,Hp=0.0M,Th=10.0C,Vh=0.0N,Vs=13.4V,Vr=3.485V,Id=Hel
02062012025200 0R0,Hp=0.0M,Th=10.0C,Vh=0.0N,Vs=13.4V,Vr=3.485V,Id=Hel
02062012025300 0R0,Hp=0.0M,Th=10.6C,Vh=0.0N,Vs=13.4V,Vr=3.485V,Id=Hel
02062012025400 ,Hp=0.0M,Th=10.0C,Vh=0.0N,Vs=13.4V,Vr=3.485V,Id=Hel
02062012025500 Vs=13.4V,Vr=3.484V,Id=Hel
02062012025600 0R0,Hp=0.0M,Th=10.0C,Vh=0.0N,Vs=13.4V,Vr=3.485V,Id=Hel
02062012025700 9C,Hp=0.0M,Th=10.0C,Vh=0.0N,Vs=13.4V,Vr=3.484V,Id=Hel
02062012025800 0.0M,Th=10.1C,Vh=0.0N,Vs=13.4V,Vr=3.484V,Id=Hel
02062012025900 =Hel
02062012026000 p=0.0M,Th=10.8C,Vh=0.0N,Vs=13.5V,Vr=3.484V,Id=Hel
The first four lines complete.
The next lines contain errors.
I want to extract for each parameter (Time,Th,Vh,Vs,Vr) an array with his values at each line. Where there is no value Nan could be ok.
I tryed with readtext.m but without results.
Thank you

채택된 답변

Pedro Villena
Pedro Villena 2012년 10월 22일
편집: Pedro Villena 2012년 10월 23일
fid=fopen('test_data.txt');
str = textscan(fid,'%s %s\r',inf);
fclose(fid);
timeStr = cell2mat(str{1});
Time = str2num(timeStr(:,9:end)); %%last 5 digits
for i=1:length(str{1}),
dataStr = cell2mat(str{2}(i));
id = regexp(dataStr,{'Th=','Vh=','Vs=','Vr='});
for j=1:length(id),
if ~isempty(id{j}),
Data(i,j) = sscanf(dataStr(id{j}+3:end),'%f');
else
Data(i,j) = Data(i-1,j);
end
end
end

추가 답변 (1개)

Umberto
Umberto 2012년 11월 12일
Thank you,
sorry for the dalay! I tested the function for long time and than I forgot to answer.
Thank again! Umberto

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by