having trouble importing data from a file

조회 수: 3 (최근 30일)
Maximilian Berndt
Maximilian Berndt 2018년 4월 13일
답변: Maximilian Berndt 2018년 4월 13일
I am having this function that was generated by matlab using the Import data tool. I am trying to Import a 4 column *.txt file into 4 seperate vectors.
function [Time,Displacement,Deceleration,Force] = importfile(filename, startRow, endRow) %IMPORTFILE Import numeric data from a text file as column vectors. % [TIME,DISPLACEMENT,DECELERATION,FORCE] = IMPORTFILE(FILENAME) Reads % data from text file FILENAME for the default selection. % % [TIME,DISPLACEMENT,DECELERATION,FORCE] = IMPORTFILE(FILENAME, STARTROW, % ENDROW) Reads data from rows STARTROW through ENDROW of text file % FILENAME. % % Example: % [Time,Displacement,Deceleration,Force] = importfile('e05_2,0_L20_W95_S35_01.txt',1, 1032); % % See also TEXTSCAN.
% Auto-generated by MATLAB on 2018/04/13 10:06:37
%% 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%[^\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);
%% 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,3,4] % 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® = {NaN}; % Replace non-numeric cells
%% Allocate imported array to column variable names Time = cell2mat(raw(:, 1)); Displacement = cell2mat(raw(:, 2)); Deceleration = cell2mat(raw(:, 3)); Force = cell2mat(raw(:, 4));
If I run the script like that I get this error:
Not enough input arguments.
Error in test (line 29) fileID = fopen(filename,'r');
Any idea what I am doing wrong? Help would be highly appreciated.
regards

답변 (1개)

Maximilian Berndt
Maximilian Berndt 2018년 4월 13일
if true
%function [Time,Displacement,Deceleration,Force] = importfile(filename, startRow, endRow)
%IMPORTFILE Import numeric data from a text file as column vectors.
% [TIME,DISPLACEMENT,DECELERATION,FORCE] = IMPORTFILE(FILENAME) Reads
% data from text file FILENAME for the default selection.
%
% [TIME,DISPLACEMENT,DECELERATION,FORCE] = IMPORTFILE(FILENAME, STARTROW,
% ENDROW) Reads data from rows STARTROW through ENDROW of text file
% FILENAME.
%
% Example:
% [Time,Displacement,Deceleration,Force] = importfile('e05_2,0_L20_W95_S35_01.txt',1, 1032);
%
% See also TEXTSCAN.
% Auto-generated by MATLAB on 2018/04/13 10:06:37
%% 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%[^\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);
%% 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,3,4] % 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® = {NaN}; % Replace non-numeric cells
%% Allocate imported array to column variable names Time = cell2mat(raw(:, 1)); Displacement = cell2mat(raw(:, 2)); Deceleration = cell2mat(raw(:, 3)); Force = cell2mat(raw(:, 4)); end

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by