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

3 vues (au cours des 30 derniers jours)
Ivan Mich
Ivan Mich le 14 Avr 2020
Commenté : Ameer Hamza le 19 Avr 2020
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?

Réponse acceptée

Ameer Hamza
Ameer Hamza le 14 Avr 2020
Modifié(e) : Ameer Hamza le 14 Avr 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 commentaires
Ivan Mich
Ivan Mich le 19 Avr 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 le 19 Avr 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

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by