首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MATLAB读取文件

MATLAB读取文件
EN

Stack Overflow用户
提问于 2013-10-08 11:27:58
回答 2查看 1.7K关注 0票数 0

我在读取文件时遇到了困难,基本上,我想摆脱不必要的文本,只打印一个只涉及数字的矩阵。

1 1 -1 1 -1 1 1

1 -1 1 -1 1 1

斯格夫

1 1 1

子虚乌有

1 -1 -1 -1 -1 -1 -1 -1 -1

1 1 -1 1 -1 -1 -1 1

1 -1 1 1

到目前为止,我尝试的是:

D= fopen('transmission_data.txt')

R=文本扫描(d,'%f %f','headerLines',3:5)

关闭(D)

但这是行不通的,因为我只需要为文本扫描输入一个数字,例如,'3',这将去掉前3行,但我想具体地去掉第三行和第五行。也许还有别的方法来读取数据?如能提供帮助,将不胜感激:)

*请注意,在第一行文本和第二行数字之间有一行空行。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-08 12:25:22

通过fileread读取该文件,用regexp将其拆分,并要求textscan查找所有数字:

代码语言:javascript
复制
C = regexp(fileread('transmission_data.txt'), '(\n|\r)*', 'split');
C = C(~cellfun('isempty', C));
D = cellfun(@(c) textscan(c, '%f'), C);
R = [D{:}].';

这里最重要的一点是,当textscan遇到一个不包含数字的行时,它会返回一个空矩阵,所以当您连接生成的向量时,只会得到来自非字符串行的向量。对于您的示例,此代码返回

代码语言:javascript
复制
>> R
R =
     1     1    -1     1     1    -1    -1     1     1     1    -1     1
     1    -1     1    -1    -1     1     1     1     1    -1     1     1
     1     1     1    -1     1    -1     1    -1    -1    -1    -1     1
     1    -1     1    -1    -1    -1     1     1    -1    -1    -1     1
     1     1    -1     1     1    -1    -1    -1     1    -1     1    -1
     1    -1     1     1     1     1     1    -1    -1    -1     1    -1
票数 0
EN

Stack Overflow用户

发布于 2013-10-08 11:49:45

有一种方法可以做到:

  1. 用大蜥蜴
  2. 按所需形式选择单元格数组或矩阵。
  3. 选择删除值无效的行
  4. 单击“导入”

如果你必须让它自动化,你可以让入口蜥蜴为你做代码生成。

下面是importw火龙在名为test.txt的文件中对数据使用时生成的(相当冗长的)代码

代码语言:javascript
复制
%% Import data from text file.
% Script for importing data from the following text file:
%
%    \\invol-vs-fp1\Users$\d.jaheruddin\MATLAB\test.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 2013/10/08 14:39:30

%% Initialize variables.
filename = 'test.txt';
delimiter = ' ';

%% 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%[^\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, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true,  '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 = [dataArray{:,1:end-1}];
numericData = NaN(size(dataArray{1},1),size(dataArray,2));

for col=[1,2,3,4,5,6,7,8,9,10,11,12]
    % 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


%% Exclude rows with non-numeric cells
J = ~all(cellfun(@(x) (isnumeric(x) || islogical(x)) && ~isnan(x),raw),2); % Find rows with non-numeric cells
raw(J,:) = [];

%% Create output variable
test = cell2mat(raw);
%% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw numericData col rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me J;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19246505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档