MATLAB: Import loop for a files in a folder

11.6

I have this function to read my file but I need a loop to import all of them at once from one folder with the same format.
function cell1 = importfile2(filename, dataLines)
%IMPORTFILE2 Import data from a text file
% CELL1 = IMPORTFILE2(FILENAME) reads data from text file FILENAME for
% the default selection. Returns the data as a table.
% CELL1 = IMPORTFILE2(FILE, DATALINES) reads data for the specified row
% interval(s) of text file FILENAME. Specify DATALINES as a positive
% scalar integer or a N-by-2 array of positive scalar integers for
% dis-contiguous row intervals.
% Example:
% cell1 = importfile2("C:\Users\admin\Desktop\all data\ty_1.DAT", [7, Inf]);
%% Input handling
% If dataLines is not specified, define defaults
if nargin < 2
dataLines = [7, Inf];
end
%% Setup the Import Options
opts = delimitedTextImportOptions("NumVariables", 7);
% Specify range and delimiter
opts.DataLines = dataLines;
opts.Delimiter = " ";
% Specify column names and types
opts.VariableNames = ["VarName1", "Date", "VarName3", "VarName4", "VarName5", "VarName6", "VarName7"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
% Import the data
cell1 = readtable(filename, opts);
end

Best Answer

Greetings,
I am not quite sure what you have going on in this code:
b = 55;
for i=1:b
[pixel, I(:,i)] = textread (['cell_', num2str(i),'.DAT'],'%f%f','headerlines' ,16);
[pixel, I0(:,i)] = textread (['nocell_', num2str(i),'.DAT'],'%f%f','headerlines' ,16);
end
But I am certainly not very experienced in reading files, though I have been able to automatically generate sets of file names in the past for saving data and then evaluating it later. Below is an example of how I accomplished the automatic filename generation:
b = 5;
baseFileName1 = 't_';
baseFileName2 = 'e_'
for i = 1:b
fileName1{i} = strcat(baseFileName1,num2str(i),'.dat')
fileName2{i} = strcat(baseFileName2,num2str(i),'.dat')
end
The example code above will generate your file names and store them in a cell array, I am not sure if that is exactly what you are looking for but I hope that it at least points you in the right direction~