MATLAB: Table name from input to loop

loopMATLABnametable

Hi,
I'm loading data from different files in a loop. I want to create a table for each individual data file.
datafiles=recursivedir('PATH','*.txt');
for o = 1:length(datafiles)
%extract date and time from filename
[versn, name, ext] = fileparts(datafiles{o});
filename=datafiles{o};
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'TextType', 'string', 'ReturnOnError', false);
fclose(fileID);
T_'name' = table(dataArray{1:end-1}, 'VariableNames', {'Output','Date','Time'});
% I want to create a new table for each loop with the name set from the fileparts.
% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans;
end
I want to create a table for each loop that has the name like T_test1 and from the fileparts name(1) = 'test1' How do I do this, so I get a table for each loop?
Best regards, Jesper

Best Answer

Rather than magically defning variable names it is much simpler to use basic MATLAB indexing and a cell array T:
D = 'Path';
S = dir(fullfile(D,'*.txt'));
T = cell(1,numel(S));
for k = 1:numel(S)
[fid,msg] = fopen(fullfile(D,S(k).name));
assert(fid>=3,msg)
C = textscan(fid,...);
fclose(fid);
T{k} = cell2table(C,...);
end