Hoe to Empty Line Discover!

2 ビュー (過去 30 日間)
dmfwlansejr
dmfwlansejr 2022 年 11 月 8 日
コメント済み: Chris 2022 年 11 月 9 日
Hi-
I have text file (T.txt).
"2" position is blank. I want to discover position "2" (Line 2)
fid = fopen('T.txt','r');
tline = fgetl(fid);
?
-----------------------------------------------------------------------
I'm about to discover that line two is empty.
What should I do?

回答 (1 件)

Chris
Chris 2022 年 11 月 9 日
if isempty(tline)
% do something, or nothing?
end
  2 件のコメント
dmfwlansejr
dmfwlansejr 2022 年 11 月 9 日
Yes, I found folowing method
for n=1:100
tline= fgetl(fid);
if isempty(tline)==1
Empty_line=n
end
end
But, my catched text data is data=';a;b;0110100...." <-I'm use [R,count] = fscanf(fid,'%s',inf);
original format is
;a
;b
;0
Empty line
1
1
0
.
.
.
Chris
Chris 2022 年 11 月 9 日
I see both a loop, and an fscanf which reads the whole file. I will continue with the loop.
Try a while loop.
feof means "end of file". ~feof means the loop hasn't finished reading the file yet.
First initialize some empty variables, and iterate or concatenate them in the loop.
n = 0;
Empty_line = [];
data = strings(0);
fid = fopen('T.txt','r');
while ~feof(fid)
n = n+1;
tline = fgetl(fid);
if isempty(tline)==1
% One way to concatenate:
Empty_line = cat(1, Empty_line, n);
else
% Another way to concatenate:
data = [data; tline];
end
end
fclose(fid);
For the data, you could use a string array, and concatenate. Before the loop, initialize it with

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by