How can I use Import Data function on multiple files? (Creating variables from multiple files in a loop?)

5 views (last 30 days)
Hi MATLAB Central,
I'd like to use the Import Data quickbutton to generate a function which extracts data from multiple files.
To generate the IMPORTDATA function for a certain file, I start generating the script by highlighting the data, then clicking 'Import Selection'>> 'Generate Function'.
The code below is what I generate for a single import, here on file '7Feb14_Alg_Only_136p_1.txt'. It will store two columns of data with the variables 'Henckystrain' and 'EngineeringStress'.
I'd like this to operate on multiple files within a folder and change the variables continuously to reflect the file name;
e.g. Here variables would be saved as something like 'Henckystrain_136p_1' and 'EngineeringStress_136_1'.
Would this be possible with some sort of loop that operates on files in the folder and continuously saves data columns into variables?
I imagine it would go like:
%For all the files in folder('folder_ex')
%Perform IMPORT FILE COMMAND BELOW
%Save the variables of IMPORTFILE as a concatenation of the file name and 'Stress' or 'Strain';
Any help on functions or documentation would be appreciated. Thanks!
Mike
function [Henckystrain,EngineeringStress] = importfile(filename, startRow, endRow)
%IMPORTFILE Import numeric data from a text file as column vectors.
% [HENCKYSTRAIN,ENGINEERINGSTRESS] = IMPORTFILE(FILENAME) Reads data from
% text file FILENAME for the default selection.
%
% [HENCKYSTRAIN,ENGINEERINGSTRESS] = IMPORTFILE(FILENAME, STARTROW,
% ENDROW) Reads data from rows STARTROW through ENDROW of text file
% FILENAME.
%
% Example:
% [Henckystrain,EngineeringStress] = importfile('7Feb14_Alg Only_
% 136p_1.txt',1, 444);
%
% See also TEXTSCAN.
% Auto-generated by MATLAB on 2014/02/09 16:48:23
%%Initialize variables.
delimiter = '\t';
if nargin<=2
startRow = 1;
endRow = inf;
end
%%Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%*s%*s%*s%*s%s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*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.
textscan(fileID, '%[^\n\r]', startRow(1)-1, 'ReturnOnError', false);
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'ReturnOnError', false);
for block=2:length(startRow)
frewind(fileID);
textscan(fileID, '%[^\n\r]', startRow(block)-1, 'ReturnOnError', false);
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'ReturnOnError', false);
for col=1:length(dataArray)
dataArray{col} = [dataArray{col};dataArrayBlock{col}];
end
end
%%Close the text file.
fclose(fileID);
%%Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[1,2]
% Converts strings in the input cell array to numbers. Replaced non-numeric
% strings with NaN.
rawData = dataArray{col};
for row=1:size(rawData, 1);
% Create a regular expression to detect and remove non-numeric prefixes and
% suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData{row}, regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if any(numbers==',');
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(thousandsRegExp, ',', 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric strings to numbers.
if ~invalidThousandsSeparator;
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch me
end
end
end
%%Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
%%Allocate imported array to column variable names
Henckystrain = cell2mat(raw(:, 1));
EngineeringStress = cell2mat(raw(:, 2));

Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!