Splitting integers and floating values from a string in MATLAB
2 ビュー (過去 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!