MATLAB: How to extract numeric values form multiple text files into a single dynamic array

cell arraycell2mattextscan

I have a folder with numberous text files, each file have some numeric data of interst that I'm trying to extract, with the data from other files, into a single big array. Here's my approach:
dr = uigetdir()
d= dir([dr,'\*.txt'])
N = length(d)
A = zeros(1000,N)
for k = 1:N
[fileLIST, FOLDER] = fopen(d(k).name, 'r');
if fileLIST<0, error(['Failed fopen: ' d(k).name FOLDER]), end
A(:,k)= textscan(fileLIST, '%f%f%*s%*s%*s%*s%*s%*s%*s%[^\n\r]', ...
'Delimiter', '\t', 'headerlines', 3, 'ReturnOnError', false);
fclose(fileLIST);
end
I have to use textscan with these parameters to get my data of interest from txt files, however I keep getting 'Conversion to double from cell is not possible'. I tried to wrap textscan inside a cell2mat, and it's not working either (All contents of the input cell array must be of the same data type). Can you please help me figure that out? Thanks!

Best Answer

It's always easier if we can see a piece of a file so know exactly what the form is, but...
Your error is that you're trying to assign cell array with ordinary array subscripting as written.
dr = uigetdir();
d= dir(fullfile(dr,'*.txt');
N = length(d);
A = cell(N,1); % collect each file output in cell array
fmt=repmat('%f',1,2); % ACTUAL FORMAT OF FILE ISN"T WHAT SHOWN ABOVE--DPB
for k = 1:N
fid = fopen(fullfile(dr,d(k).name), 'r'); % fopen returns a file handle, not a string
if fid<0, error(['Failed fopen: ' fullfile(dr,d(k).name)]), end
A(k)= textscan(fid, fmt, 'Delimiter', '\t', ...
'headerlines', 2, ...
'CollectOutput', 1);
fid=fclose(fid);
end
A=cell2mat(A); % now convert the whole shebang