i am having trouble reading the file, basically, i want to somehow get rid of the unnecessary text and just print out a matrix involving only the numbers.

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

what i've tried so far is:

d = fopen('transmission_data.txt')

R = textscan(d, '%f %f', 'headerLines', 3:5)

fclose(d)

but this doesnt work, as i have to put only one number for the textscan, for example, '3', this would get rid of the first 3 lines, but i want to specifically get rid of the third and fifth. maybe there's some other way to read data? Help would be appreciated :)

*note that there is an empty line between the first line of text and the second row of numbers

有帮助吗?

解决方案

Read the file by fileread, split it it by regexp and ask textscan to find all the numbers:

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

The important point here is that when textscan encounters a row that doesn't contain numbers it returns an empty matrix so when you concatenate the resulting vectors you just get the ones that come from non-string rows. For your example this code returns

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

其他提示

Here is one way to do it:

  1. Use the importwizard
  2. Select cell array or matrix as desired form
  3. Select remove rows with invalid values
  4. Click import

If you have to automate it, you can let the importwizard do code generation for you.


Here is the (rather verbose) code generated by the importwizard when I use it on your data in a file called 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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top