将一系列文本文件导入Matlab并将数据保存为矩阵(变量)

时间:2015-02-03 12:58:03

标签: matlab function text import edit

我有一个包含.txt文件的文件夹。我想将这些导入Matlab。我使用了'import data'工具和sucsess,但当时只用于一个文件。

我想有一个方法来处理文件夹中的所有文件并将它们保存为变量,可能使用数字(1toX)作为名称。文件名不是顺序的。

我希望我可以使用和编辑导入1个单个文件时创建的函数/脚本,并生成它以自动导入文件夹中的所有文件。这是因为我在导入1个文件时得到了我想要的。只是不知道如何一次导入目录中的所有文件? (有些文件是.dat)。所有.txt文件的文件格式都相同。文件的第一行:

    ----------------------------------------------------------------------------------------------
BeanSensor AX-3D

Mac Id : 00158D00000XX
Network Id : 00XX
Pan Id : 23FB
Sensor Id : 0
Sensor Label : Ch_X

Ratio : 1
Offset : 0
Unit : g

Date : 17.09.2014 05:32:24.000
Data acquisition cycle : 3604
Data acquisition duration : ~3600
Sampling rate : 100
----------------------------------------------------------------------------------------------
Measure Index;Measure Value

0;-0,0152
1;-0,0118
3;-0,0057
4;-0,0091
5;-0,0102
6;-0,0129
7;-0,0106
8;-0,0057
9;-0,0091
10;-0,0083
11;-0,0095
12;-0,0064
13;-0,0122
14;-0,0141

这是来自iport工具的脚本:

%% Import data from text file.
% Script for importing data from the following text file:
%
%    C:\DATA\Dolmsundet\Streaming_0_x_0_0_x_00158D00000E03B0_17-09-2014_05.36.09_part000.txt
%
% To extend the code to different selected data or a different text file,
% generate a function instead of a script.

% Auto-generated by MATLAB on 2015/02/03 13:48:57

%% Initialize variables.
filename = 'C:\DATA\Dolmsundet\Streaming_0_x_0_0_x_00158D00000E03B0_17-09-2014_05.36.09_part000.txt';
delimiter = ';';
startRow = 17;

%% Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%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, 'ReturnOnError', false);
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false);

%% 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 = strrep(numbers, '.', '');
                numbers = strrep(numbers, ',', '.');
                numbers = textscan(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

%% Create output variable
Streaming0x00x00158D00000E03B01709201405 = cell2mat(raw);
%% Clear temporary variables
clearvars filename delimiter startRow formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me R;

希望这是可以理解的。 所有帮助表示赞赏:)

0 个答案:

没有答案