import loop for a files in a folder
Mostra commenti meno recenti
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 Commenti
Rik
il 11 Giu 2019
What is your specific question? Do you have trouble getting a file list based on your folder?
Rik
il 11 Giu 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.
Risposta accettata
Più risposte (1)
James Browne
il 13 Giu 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 Commento
FSh
il 13 Giu 2019
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
