2013-10-08 49 views
0

我无法读取文件, 基本上,我想以某种方式摆脱不必要的文本,只是打印出一个只涉及数字的矩阵。MATLAB读取文件

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

1 -1 1 -1 -1 1个1 1 1 -1 1 1

sgfgdf

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

rtydsfdsfds

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 = textscan(d, '%F%F', 'headerLines',3:5)

FCLOSE(d)

但这不起作用,因为我不得不为文本只输入一个数字可以,例如,'3',这将摆脱前3行,但我想特别摆脱第三和第五。 也许有其他方法来读取数据? 帮助,将不胜感激:)

*注意,文本的第一行和数字

回答

0

文件由fileread读取文件的importwizard产生的(相当冗长)代码,把它分解它regexp并要求textscan找到所有的数字:在这里

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

重要的一点是,当textscan encounte rs不包含数字的行会返回一个空矩阵,因此当您连接结果向量时,您只需获取来自非字符串行的行。 对于示例此代码返回

>> 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

我似乎得到一个错误 ???错误使用==> textscan 第一个输入不能为空。 错误==> @(c)中textscan(C, '%F') 错误==> Q1A在38 d = cellfun(@(c)中textscan(C, '%F'), C); –

+0

@GeorgeRandall出于某种原因在'C'中有空字符串。我添加了一行来删除这些。它现在应该工作。 –

0

这里的第二行之间的空行做到这一点的一种方法:

  1. 使用所述importwizard
  2. 选择细胞阵列或矩阵为所需的形式
  3. 选择删除与无效值的行
  4. 单击导入

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


下面是当我使用它在你的数据在一个名为test.txt的

%% 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

不亚于我想用importwizard,我确实有使用需要我用另一种方式写代码 –

+0

@GeorgeRandall如果你想编写代码,我建议你根据导入向导生成的代码进行编译,可能在核心使用'importdata'的一些行。如果您无法解释代码,请尝试阅读此命令。 –

+0

importwizard和importdata不起作用,因为它只能读取数据的前2行 –