MATLAB: Reading numerical data from text fles with string

column creatornumerical values separator

Dear all,
I will really appreciate f you help me out in this regards. I am using follwing code to read multiple text files and genrating a data based on specific columns. I now wanted to read the numeric values from line 10 and 11 and place them in first and column of data and repeat them for all the values of a specific cell array. Based on new cell array, the values should change till the end of that array and so on…..
clear;
D = 'T:\Intelli_test\New\files';
S = dir(fullfile(D, '*.txt'));
N = length(S);
% A = cell(N,1);
fmt = '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f';
for k = 1:N
fid = fopen(fullfile(D,S(k).name), 'r');
A(k)= textscan(fid, fmt, 'Delimiter', '\t', …
'headerlines', 33, …
'CollectOutput', 1);
fid=fclose(fid);
M = vertcat(A{:});
out = M(:,[9,11]);
data=out;
end
i am now trying to add a loop like this to get the values but i am getting nothing
for k = 1:N
fid = fopen(fullfile(D,S(k).name), 'r');
linenum = 10;
h(k)= textscan(fid,'%f',1,'delimiter','\n', 'headerlines',linenum-1);
fid=fclose(fid);
end

Best Answer

hello
my suggestion for the second part of your code , assuming you specify the lines to be read :
n_lines = [10 11]; % define lines to read
for k = 1:N
fid = fopen(fullfile(D,S(k).name), 'r'); % open file
result = cell(1,numel(n_lines));
for n = 1:numel(n_lines)
result(n) = textscan(fid, '%s', 1, 'Headerlines', n_lines(n)-1, 'Delimiter' ,'');
frewind(fid) % set file position back to the start
end
result = [result{:}]; % unbox from cells
fclose(fid); % close file
end