MATLAB Answers

how to obtain the number of lines starting with a given string from an external text file and save the numeric value of variables of those lines

2 ビュー (過去 30 日間)
Hugo
Hugo 2020 年 9 月 28 日
コメント済み: Hugo 2020 年 9 月 29 日
Hello,
I have an external text file. A part of the text file (example) is shown below:
/AA
/BB
/CC
DRL=1
DRL=2
DRL=4
/XX
/YY
/ZZ
How can I :
-Obtain the number of lines starting with "DRL" and save it to the variable pnumber. In this case pnumber=3
-save the value of those variables in the vector A, in my example A =[1;2;4]
I thank you in advance for any help,
Best regards,
Hugo

  0 件のコメント

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

採用された回答

Sudheer Bhimireddy
Sudheer Bhimireddy 2020 年 9 月 28 日
Another approach:
data = readtable('test.txt'); % test.txt contains the given text
drl_count = sum(strcmp(data.Var1,'DRL'));
drl_value = data.Var2(strcmp(data.Var1,'DRL'));
>> drl_count =
3
drl_value =
1
2
4

  7 件のコメント

表示 4 件の古いコメント
Hugo
Hugo 2020 年 9 月 29 日
Nice!!! Thank you! It works. But now, I have other problems.
-How can read only the first 10 lines of the text file?
-But first I think I have to convert the original file, in .lgw, to .txt, unless other function than readtable is used.
Thank you once again.
Sudheer Bhimireddy
Sudheer Bhimireddy 2020 年 9 月 29 日
I would say first, try fopen with the .lgw and if it didn't work then convert to txt file.
Try this:
file_ID = fopen('data2.txt','rt'); % Opens file in text mode
% Number of lines to read
nLine_limit = 8;
% Pre-allocate
data{nLine_limit,1} = 0;
nLine = 1;
% Read first few lines based on nLine_limit
while nLine <= nLine_limit
data{nLine,1} = fgetl(file_ID); % Read the entire line
nLine = nLine + 1;
end
fclose(file_ID);
% Filter out the lines with DRL in them and get their indices
drl_Line_Mask = ~cellfun(@isempty, regexp(data,'DRL','match'));
% Seperate all DRL lines
drl_data = data(drl_Line_Mask);
% Split the cells based on '='
drl_split = regexp(drl_data,'=','split');
% Count number of lines with DRL
drl_count = numel(drl_split);
% Pre-allocate
drl_value(drl_count,1) = nan;
for i=1:size(drl_split)
drl_value(i,:) = str2double(drl_split{i,1}{1,2});
end

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

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 9 月 28 日
編集済み: Ameer Hamza 2020 年 9 月 28 日
Try this
f = fileread('data.txt');
lines = textscan(f, '%s');
lines = lines{1};
idx = cellfun(@(x) x(1)=='/', lines);
lines(idx) = [];
pnumber = numel(lines);
A = cellfun(@(x) sscanf(x, 'DRL=%d'), lines);
data.txt is attached.

  2 件のコメント

Hugo
Hugo 2020 年 9 月 28 日
Hello,
Thanks for your reply.
I am getting an error with your approach:
Error using textscan
First input can not be empty. Expected a non-empty character vector or a valid file-id.
Error in calc_test (line 2)
lines = textscan(f,'%s');
Ameer Hamza
Ameer Hamza 2020 年 9 月 28 日
Have you placed the file data.txt in that folder? data.txt should contain the text in the question.

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

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by