import loop for a files in a folder

2 views (last 30 days)
FSh
FSh on 11 Jun 2019
Commented: FSh on 13 Jun 2019
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
  3 Comments
FSh
FSh on 11 Jun 2019
Edited: Rik on 11 Jun 2019
sorry, I uploaded in answer part.
Edit Rik: moved content to comment:
I want to import files in a table format for further processing . I need a loop for 2 diffrent file name like as (t_1.dat, t_2.dat .....t_55.dat) and (e_1.dat, ..., e_55.dat). I have this code but it does not work, I think because of the star sign (with these errors
Error using dataread/Trouble reading floating point number from file (row 1, field 1)
Error in textread (line 171)
[varargout{1:nlhs}]=dataread('file',varargin{:}); %#ok<REMFF1>
Error in Untitled (line 5)
[pixel, I(:,i)] = textread (['cell_', num2str(i),'.DAT'],'%f%f','headerlines' ,16);
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
Rik
Rik on 11 Jun 2019
First you should make sure your script works on 1 file, then you can probably extend it fairly easily. It might be a better idea to read the entire file as text, and then parse it to a table yourself.

Sign in to comment.

Accepted Answer

James Browne
James Browne on 12 Jun 2019
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~
  4 Comments
Rik
Rik on 13 Jun 2019
Are there empty lines at the end of the file?
FSh
FSh on 13 Jun 2019
But I attached the first part of the table.However many thanks for the reponse.

Sign in to comment.

More Answers (1)

James Browne
James Browne on 13 Jun 2019
Thank you it works greatly to import but the reading part is missed . Every time I will get this error.
=-========================
>> Untitled12
Error using dataread
Number of outputs must match the number of unskipped input fields.
I think your problem is in this line:
[pixel, I(:,i)] = textread (['cell_', num2str(i),'.DAT'],'double','headerlines' ,16);
My guess is you are trying to read 16 rows in the file, ignoring one line (the header)? The line of code above, however is asserting that there are 16 rows to be ignored (number of header rows = 16).
Perhaps, try something like:
[pixel, I(:,i)] = textread (['cell_', num2str(i),'.DAT'],'double','headerlines' ,1,16);
  1 Comment
FSh
FSh on 13 Jun 2019
b = 5;
baseFileName1 = 'cell_';
baseFileName2 = 'nocell_'
for i = 1:b
fileName1{i} = strcat(baseFileName1,num2str(i),'.dat','%f%f','headerlines' ,16)
fileName2{i} = strcat(baseFileName2,num2str(i),'.dat','%f%f','headerlines' ,16)
end
this code workes but how can I check if the tables are readable. Because there are no list of the tables in workspace.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!