필터 지우기
필터 지우기

Splitting integers and floating values from a string in MATLAB

조회 수: 2 (최근 30일)
Teja Reddy
Teja Reddy 2022년 12월 21일
답변: Stephen23 2022년 12월 22일
Hi,
Thank you all.
I would like to read a values from a string which has text, values, space, etc., as shown below.
I would like to take only integer and floating values in the shown content shown in bold text.
There is one space in the starting of each line in the string of bold text.
I donot require any other text in the following.
File C:\Users\Black\Desktop\
TABLE: "PROGRAM CONTROL"
ProgramName=aa Version=21.2.0 ProgLevel=Ultimate LicenseNum=3010*1D7V2883PA2XZJW LicenseOS=Yes LicenseSC=Yes LicenseHT=No CurrUnits="C" SteelCode="b" ConcCode="ACI 318-14" AlumCode="d" _
ColdCode=AISI-ASD96 RegenHinge=Yes
TABLE: "movements"
k=1 Type=NonDirHist StepType=Time StepNum=0 C1=0 C2=0 U3=0 G1=0 R2=0 R3=0
k=1 ype=NonDirHist StepType=Time StepNum=0.1 C1=0 C2=0 U3=0 G1=0 R2=0.0224418047676415 R3=0
k=1 Type=NonDirHist StepType=Time StepNum=0.2 C1=0 C2=0 U3=0 G1=0 R2=0.0261030403456047 R3=0
k=1 Type=NonDirHist StepType=Time StepNum=0.3 C1=0 C2=0 U3=0 G1=0 R2=0.0352000343350302 R3=0
END TABLE DATA
Thank you
  댓글 수: 4
Stephen23
Stephen23 2022년 12월 21일
Is the spelling mistake on the second line of the table really in the original data? (Type -> ype)
Teja Reddy
Teja Reddy 2022년 12월 21일
Yes, I am sorry, there is a spelling mistake.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2022년 12월 22일
Here an approach which returns a table, which might be more useful for accessing your data:
T = readtable('ex.txt', 'NumHeaderLines',5, 'Delimiter',{'=',' '}, ...
'MultipleDelimsAsOne',true, 'LeadingDelimitersRule','ignore',...
'MissingRule','omitrow')
T = 4×20 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 _____ ____ ________ ______________ ____________ ________ ___________ ____ ______ _____ ______ _____ ______ _____ ______ _____ ______ ________ ______ _____ {'k'} 1 {'Type'} {'NonDirHist'} {'StepType'} {'Time'} {'StepNum'} 0 {'C1'} 0 {'C2'} 0 {'U3'} 0 {'G1'} 0 {'R2'} 0 {'R3'} 0 {'k'} 1 {'ype' } {'NonDirHist'} {'StepType'} {'Time'} {'StepNum'} 0.1 {'C1'} 0 {'C2'} 0 {'U3'} 0 {'G1'} 0 {'R2'} 0.022442 {'R3'} 0 {'k'} 1 {'Type'} {'NonDirHist'} {'StepType'} {'Time'} {'StepNum'} 0.2 {'C1'} 0 {'C2'} 0 {'U3'} 0 {'G1'} 0 {'R2'} 0.026103 {'R3'} 0 {'k'} 1 {'Type'} {'NonDirHist'} {'StepType'} {'Time'} {'StepNum'} 0.3 {'C1'} 0 {'C2'} 0 {'U3'} 0 {'G1'} 0 {'R2'} 0.0352 {'R3'} 0
C = cellstr(mode(categorical(T{:,1:2:end}),1));
T = T(:,2:2:end);
T.Properties.VariableNames = C
T = 4×10 table
k Type StepType StepNum C1 C2 U3 G1 R2 R3 _ ______________ ________ _______ __ __ __ __ ________ __ 1 {'NonDirHist'} {'Time'} 0 0 0 0 0 0 0 1 {'NonDirHist'} {'Time'} 0.1 0 0 0 0 0.022442 0 1 {'NonDirHist'} {'Time'} 0.2 0 0 0 0 0.026103 0 1 {'NonDirHist'} {'Time'} 0.3 0 0 0 0 0.0352 0

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2022년 12월 21일
hello
try this
Str = readlines('ex.txt');
ind1 = find(contains(Str,'TABLE: "movements"'));
ind2 = find(contains(Str,'END TABLE DATA'));
Str = Str(ind1+1:ind2-1);
B = regexp(Str,'\d+(\.)?(\d+)?','match');
% convert string to num array
for ci = 1:numel(B)
out(ci,:) = str2double(B{ci});
end

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by