How to read line by line a cell file (3x1) using a loop?

4 views (last 30 days)
Hello,
I have a .txt file. I use the command
regexp(fileread('file1.txt'), '\r?\n', 'split') .';
in order to open it. This file contains 3 lines (it is a 3x1cell). I would like to read line by line each line of this file, by using a loop.
Could anyone help me?

Accepted Answer

Ameer Hamza
Ameer Hamza on 14 Apr 2020
Edited: Ameer Hamza on 14 Apr 2020
For-loop version
fid = fopen('test.txt');
data = {};
while true
line_data = fgetl(fid);
if line_data == -1
break;
end
data{end+1} = line_data;
end
data = data';
fclose(fid);
  18 Comments
Ivan Mich
Ivan Mich on 19 Apr 2020
ok, my question is do I have to put an extra for ?
i mean
for i=1:size(data_f3,1)
for ii=1:size(data_f3{:,4},1)
if data_f3{:,4}==0
do something
else
do something
end
end
end
or it is not necessary because the number of lines is the same?
Do you understand what I mean?
Ameer Hamza
Ameer Hamza on 19 Apr 2020
I think using single for loop should be fine. Something like this
for i=1:size(data_f3,1)
if data_f3{i,4}==0
do something
else
do something
end
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!