Borrar filtros
Borrar filtros

Splitting integers and floating values from a string in MATLAB

2 visualizaciones (últimos 30 días)
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 comentarios
Stephen23
Stephen23 el 21 de Dic. de 2022
Is the spelling mistake on the second line of the table really in the original data? (Type -> ype)
Teja Reddy
Teja Reddy el 21 de Dic. de 2022
Yes, I am sorry, there is a spelling mistake.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 22 de Dic. de 2022
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

Más respuestas (1)

Mathieu NOE
Mathieu NOE el 21 de Dic. de 2022
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

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by