Create a function to extract data several csv files

12 views (last 30 days)
Hello! I´ve been trying to use the Import data/create a function to select a part of my csv files. I want to take matrix of data from a lot of different files. The layout is always the same, so I read tutorials. I try with: http://nl.mathworks.com/help/matlab/import_export/import-data-interactively.html But I get Undefined function 'importfile' for input arguments of type 'char'.
I want, if it is possible a function to use is several csv files. I want to create the matrix numbers. I have a lot of files to run. Can it be done? I think I have a problem to make matlab understand how to change to other files. If I use Import script I get the data but only for that one csv file I used. Not for others.
I am really new at these, any help is a lot! thank you.
  5 Comments
Milagros ARIETTI
Milagros ARIETTI on 20 Oct 2015
function [VarName3,VarName4] = importfile(filename, startRow, endRow) %IMPORTFILE Import numeric data from a text file as column vectors. % [VARNAME3,VARNAME4] = IMPORTFILE(FILENAME) Reads data from text file % FILENAME for the default selection. % % [VARNAME3,VARNAME4] = IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data % from rows STARTROW through ENDROW of text file FILENAME. % % Example: % [VarName3,VarName4] = importfile('WT01.csv',21, 352); % % See also TEXTSCAN.
% Auto-generated by MATLAB on 2015/10/20 13:20:25
%% Initialize variables. delimiter = ';'; if nargin<=2 startRow = 21; endRow = 352; end
%% Format string for each line of text: % column3: double (%f) % column4: double (%f) % For more information, see the TEXTSCAN documentation. formatSpec = '%*s%*s%f%f%*s%*s%*s%*s%*s%*s%[^\n\r]';
%% Open the text file. fileID = fopen(filename,'r');
%% Read columns of data according to format string. % This call is based on the structure of the file used to generate this % code. If an error occurs for a different file, try regenerating the code % from the Import Tool. dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(1)-1, 'ReturnOnError', false); for block=2:length(startRow) frewind(fileID); dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(block)-1, 'ReturnOnError', false); for col=1:length(dataArray) dataArray{col} = [dataArray{col};dataArrayBlock{col}]; end end
%% Close the text file. fclose(fileID);
%% Post processing for unimportable data. % No unimportable data rules were applied during the import, so no post % processing code is included. To generate code which works for % unimportable data, select unimportable cells in a file and regenerate the % script.
%% Allocate imported array to column variable names VarName3 = dataArray{:, 1}; VarName4 = dataArray{:, 2};
if true
% code
end
*and after that I try adding*
numFiles = 4; startRow = 21; endRow = 532; myData = cell(1,numFiles);
for fileNum = 1:numFiles fileName = sprintf('WT%01d.csv',fileNum); myData{fileNum} = importfile(fileName,startRow,endRow); end
Milagros ARIETTI
Milagros ARIETTI on 21 Oct 2015
function WT01 = importfile1(filename, startRow, endRow)
%IMPORTFILE1 Import numeric data from a text file as a matrix.
% WT01 = IMPORTFILE1(FILENAME) Reads data from text file FILENAME for the
% default selection.
%
% WT01 = IMPORTFILE1(FILENAME, STARTROW, ENDROW) Reads data from rows
% STARTROW through ENDROW of text file FILENAME.
%
% Example:
% WT01 = importfile1('WT01.csv', 21, 532);
%
% See also TEXTSCAN.
% Auto-generated by MATLAB on 2015/10/21 14:40:29
%%Initialize variables.
filename = 'C:\Users\Milagros\Desktop\Pruebas\0100\New folder\WT%d%d.csv';
delimiter = ';';
if nargin<=2
startRow = 21;
endRow = 532;
end
%%Format string for each line of text:
% column3: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%*s%*s%f%*s%*s%*s%*s%*s%*s%*s%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r');
%%Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow)
frewind(fileID);
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
dataArray{1} = [dataArray{1};dataArrayBlock{1}];
end
%%Close the text file.
fclose(fileID);
%%Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.
%%Create output variable
WT01 = table(dataArray{1:end-1}, 'VariableNames', {'VarName3'});
I've been trying different things taking the import function and the comments. I can't make it work, these last one says:
Error using textscan Invalid file identifier. Use fopen to generate a valid file identifier.
Error in scotopic (line 36) dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
Could someone help me with these.

Sign in to comment.

Accepted Answer

Milagros ARIETTI
Milagros ARIETTI on 20 Oct 2015
Thank you so much for your answers!
I will upload a file so you can see.
What I need is to take the data from different columns and rows to work with them after. Example. Take C21:J532, C556:H1067, C1089:H1600
After I will need to work on that matrix.

More Answers (2)

TastyPastry
TastyPastry on 20 Oct 2015
Place all the files you need to work with in your current directory and use
files = dir('*.csv');
This will list all the .csv files in your folder and you can loop through them using a function of your choosing.
  1 Comment
Guillaume
Guillaume on 20 Oct 2015
Edited: Guillaume on 20 Oct 2015
There is no need to place the files in the current directory and I would advise against that as it's a good way to loose some files in the process. dir happily works in any directory:
files = dir(fullfile('c:\path\to\folder', '*.csv'));

Sign in to comment.


Milagros ARIETTI
Milagros ARIETTI on 20 Oct 2015
Sorry I forgot to hit attach...

Community Treasure Hunt

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

Start Hunting!