Splitting integers and floating values from a string in MATLAB
1 次查看(过去 30 天)
显示 更早的评论
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
2022-12-21
Is the spelling mistake on the second line of the table really in the original data? (Type -> ype)
采纳的回答
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')
C = cellstr(mode(categorical(T{:,1:2:end}),1));
T = T(:,2:2:end);
T.Properties.VariableNames = C
0 个评论
更多回答(1 个)
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 Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!