Splitting integers and floating values from a string in MATLAB
2 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 21 Dec 2022
Is the spelling mistake on the second line of the table really in the original data? (Type -> ype)
Accepted Answer
Stephen23
on 22 Dec 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')
C = cellstr(mode(categorical(T{:,1:2:end}),1));
T = T(:,2:2:end);
T.Properties.VariableNames = C
0 Comments
More Answers (1)
Mathieu NOE
on 21 Dec 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
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!