MATLAB: How to read a specific row in a txt-file with variable row length

dlmreadreading rowstxt filetxt file notesvariable row length

Dear Matlab users,
Maybe someone can help me with the following:
For a project I would like to read a certain row of the txt-file constant_file_matlab.txt. In this example I would like to read the 17th row:
n=7
M = dlmread('constant_file_matlab.txt','\t',[17 0 17 n-1])
I get the following output:
M =
-5 0 15 20 40 250 0
This is the output I would like to see, but the problem is that in some cases the length of this vector will change. I tried the following:
M = dlmread('constant_file_matlab.txt','\t',[17 0 17 end])
but is not working. Therefore my questions is: how can I read one specific row in my txt-file from the first to the last value?
However, ideally I would like to do the same as mentioned above, but with text notes at the end of each row (see txt-file: constant_file_matlab2). Is there a method to read this file with notes row by row?
Thank you.

Best Answer

"For a project I would like to read a certain row of the txt-file constant_file_matlab.txt"
function S = ReadLineOfFile(FileName, L)
[fid, msg] = fopen(FileName, 'r');
assert(fid ~= -1, msg);
for k = 1:L-1
fgetl(fid); % skip one line
end
S = fgetl(fid); % reply the L.th line:
fclose(fid)
end
Now you want to parse the line:
S = '0.10 2.60 0.70 6.85 3.90 0.95 2.00 1.05 1.68 %notes'
% Actually:
% S = ReadLineOfFile('constant_file_matlab.txt', 17)
[Value, ~, ~, nextindex] = sscanf(S, '%g ', [1, inf]);
Comment = S(nextindex:end)
Related Question