Khan
Khan

Reputation: 1518

Import Data from Multiple Files using Import Tool

I want to import Multiple Files (waveforms in .wf format) using the Import Tool in MATLAB 2016a.

This is my (auto-generated) function

function Ring607062016175832 = importfile(filename, startRow, endRow)
%IMPORTFILE Import numeric data from a text file as a matrix.

%   RING607062016175832 = IMPORTFILE(FILENAME) Reads data from text file
%   FILENAME for the default selection.
%

%   RING607062016175832 = IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data
%   from rows STARTROW through ENDROW of text file FILENAME.
%
% Example:
%   Ring607062016175832 = importfile('Ring6_07-06-2016_17-58-32.wf', 1, 50001);
%
%    See also TEXTSCAN.

% Auto-generated by MATLAB on 2016/10/12 13:02:36

%% Initialize variables.
delimiter = ';';
if nargin<=2
    startRow = 1;
    endRow = inf;
end

%% Format string for each line of text:
%   column1: double (%f)
%   column2: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%f%[^\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, 'EmptyValue' ,NaN,'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow)
    frewind(fileID);
    dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'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.

%% Create output variable
Ring607062016175832 = table(dataArray{1:end-1}, 'VariableNames', {'VarName1','VarName2'});

When I run the function with input arguments such as

importfile('Ring6_07-06-2016_17-58-32.wf', 1, 50001)

I get the valid result for one imported file.

My problem:

I want to create a "for" loop to import the data from all ".wf" files. Using this example I created this "for" loop in a separate script:

numFiles = 361;
startRow = 1;
endRow = 50001;
WaveformData = cell(1,numFiles);

for fileNum = 1:numFiles
    filename = sprintf('Ring6%02d.wf',fileNum);
    WaveformData{fileNum} = importfile('filename', 1, 50001);
end

But when I try to run it, I get this error:

Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in importfile (line 36)
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'EmptyValue'
,NaN,'HeaderLines', startRow(1)-1, 'ReturnOnError', false);

Error in Schleife (line 8)
    WaveformData{fileNum} = importfile(filename, startRow, endRow);

Can anybody tell me what I did wrong or give me a hint how to create a working "for"-loop to import all the files?

Upvotes: 0

Views: 175

Answers (1)

Thomas
Thomas

Reputation: 725

Change

 WaveformData{fileNum} = importfile('filename', 1, 50001);

to

 WaveformData{fileNum} = importfile(filename, 1, 50001);

without the ' '

Upvotes: 1

Related Questions