MATLAB Answers

How to get specific rows in a text file and store it in a matrix?

1 ビュー (過去 30 日間)
Hello everyone, I have a text file of consisting data. I want to pick up specific rows starting with (PL51) and store it in a matrix.
+Any kind of help would be appreciated.
+Data sample
+ Also, you can find text file in the attachment.
* 2021 5 16 0 0 0.00000000
PL51 3237.654801 -2943.534852 11471.056095 999999.999999
VL51 -50540.516761 -31697.794702 6392.165187 999999.999999
* 2021 5 16 0 2 0.00000000
PL51 2623.316843 -3313.921643 11529.952432 999999.999999
VL51 -51818.375773 -30014.173455 3421.719481 999999.999999
* 2021 5 16 0 4 0.00000000
PL51 1994.763046 -3663.432262 11553.143593 999999.999999
VL51 -52908.945184 -28219.874309 442.832755 999999.999999
* 2021 5 16 0 6 0.00000000
PL51 1354.271051 -3990.791260 11540.583352 999999.999999
VL51 -53807.318501 -26323.742841 -2535.319935 999999.999999
* 2021 5 16 0 8 0.00000000
PL51 704.172979 -4294.831581 11492.335440 999999.999999
VL51 -54509.324800 -24334.991911 -5503.591803 999999.999999
* 2021 5 16 0 10 0.00000000
PL51 46.846515 -4574.498718 11408.573045 999999.999999
VL51 -55011.543908 -22263.160221 -8452.890926 999999.999999
* 2021 5 16 0 12 0.00000000
PL51 -615.294186 -4828.854373 11289.578001 999999.999999
VL51 -55311.318759 -20118.070018 -11374.206387 999999.999999
* 2021 5 16 0 14 0.00000000
PL51 -1279.807708 -5057.079602 11135.739656 999999.999999
VL51 -55406.764955 -17909.784089 -14258.633917 999999.999999

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 6 月 5 日
編集済み: Sulaymon Eshkabilov 2021 年 6 月 5 日
Here is a quick solution:
%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 7);
% Specify range and delimiter
opts.DataLines = [3, Inf];
opts.Delimiter = " ";
% Specify column names and types
opts.VariableNames = ["VarName1", "VarName2", "VarName3", "VarName4", "VarName5", "VarName6", "VarName7"];
opts.VariableTypes = ["categorical", "double", "double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
% Specify variable properties
opts = setvaropts(opts, "VarName1", "EmptyFieldRule", "auto");
% Import the data
lageos1 = readtable("lageos1.txt", opts);
% Select and separate data
D=lageos1;
DS = D((lageos1(:,1).VarName1=='PL51'),:); % Selected data
  1 件のコメント
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 6 月 6 日
Most Welcome Mohammad!

サインインしてコメントする。

その他の回答 (1 件)

dpb
dpb 2021 年 6 月 5 日
One of about a zillion ways...deliberately illustrating some newer features...
opt=detectImportOptions('lageos1.txt','CommentStyle',{'*'},'FileType','fixedwidth');
opt.SelectedVariableNames=opt.SelectedVariableNames(2:end);
data=readmatrix('lageos1.txt',opt);
data=data(1:2:end,:);

Community Treasure Hunt

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

Start Hunting!

Translated by