[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Matlab to Octave - problem wih the conversion

From: Istan
Subject: Matlab to Octave - problem wih the conversion
Date: Wed, 3 Aug 2016 06:57:45 -0700 (PDT)

Hello everybody,

I have used a little code on Matlab, which runs perfectly well. For few
reasons, I want to use a freeware. The high compatibility matlab/octave is
the reason why I have chosen Octave. However, my code doesn't run at all, it
seems there is a problem with textscan. If someone can help me to resolve my
problem :

The code :

% generate a function instead of a script.
% Auto-generated by MATLAB on 2016/07/29 16:20:55
%% Initialize variables.

delimiter = ' ';

%% Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%[^\n\r]';

%% Open the text file.
fileID = fopen('1.txt','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.

%% 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]
    % Converts strings in the input cell array to numbers. Replaced
    % 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 =
            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;
            % Convert numeric strings to numbers.
            if ~invalidThousandsSeparator;
                numbers = textscan(strrep(numbers, ',', ''), '%f');
                numericData(row, col) = numbers{1};
                raw{row, col} = numbers{1};
        catch me

%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric
raw(R) = {NaN}; % Replace non-numeric cells

%% Allocate imported array to column variable names
VarName1 = cell2mat(raw(:, 1));
x1 = cell2mat(raw(:, 2));

%% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw numericData
col rawData row regexstr result numbers invalidThousandsSeparator
thousandsRegExp me R;

axis([0 100 600 1600])

and the file  1.txt

Thank you for reading and help.


View this message in context:
Sent from the Octave - General mailing list archive at

reply via email to

[Prev in Thread] Current Thread [Next in Thread]