MATLAB: I need to use ‘for loop’ to read the names from a text file using ‘fgetl’ function. How to do that

fgetlfor loop

There is a list of 80 test subject names in a text file, and I have to use the for loop function to read the names of the first 40 subjects using fgetl function. How do I write the code in MATLAB.
Thanks!!

Best Answer

fid = fopen('filename.txt');
line1 = fgetl(fid);
res=line1;
while ischar(line1)
if ischar(line)
res =char(res,line1)
end
line1 = fgetl(fid);
end
fclose(fid);