Matlab to Octave - problem wih the conversion

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Matlab to Octave - problem wih the conversion

Istan
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Matlab to Octave - problem wih the conversion

Juan Pablo Carbajal-2
On Wed, Aug 3, 2016 at 3:57 PM, Istan <[hidden email]> wrote:

> 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.
> 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]
>     % 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
>
>
> %% 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
>
> %% 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;
>
> %%%%%%%%%%%%%%%
> plot(VarName1*1000,x1,'k')
> axis([0 100 600 1600])
> %%%%%%%%%%%%%%
>
> and the file  1.txt
> <http://octave.1599824.n4.nabble.com/file/n4678981/1.txt>
>
> Thank you for reading and help.
>
> Guillaume
>
>
>
> --
> View this message in context: http://octave.1599824.n4.nabble.com/Matlab-to-Octave-problem-wih-the-conversion-tp4678981.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave

Hi,

GNU Octave is not freeware, it is FOSS software. It might be good to
take a look and understand the difference. You wont make many friends
calling FOSS projects "freeware".

It seems the file you are using belongs to Mathworks and I am not sure
you are allowed to use it in Octave. Could you check the Terms of Use?
last time we checked their files are to be used only with Mathworks
products.

Cheers

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Matlab to Octave - problem wih the conversion

Sebastian Schöps
Juan Pablo Carbajal-2 wrote
It seems the file you are using belongs to Mathworks and I am not sure
you are allowed to use it in Octave. Could you check the Terms of Use?
last time we checked their files are to be used only with Mathworks
products.
just to understand this well: you say that Mathworks has rights on this code because it was automatically generated? (I know that analogies are dangerous but if I apply an image filter in photoshop Adobe does not acquire right on my image?). However, let's suposse this is the case, then one might not be allowed to distribute the code freely but why can't someone run it with Octave? I may run proprietary m-files with octave.  

@Istan: in either case, you may want to create a minimal (free) working example of your problem and then we can see what we can do...

Sebastian
Reply | Threaded
Open this post in threaded view
|

Re: Matlab to Octave - problem wih the conversion

Istan
Hi,

Thank you for your answer and these informations.

I m a new user of matlab and octave. I m sorry for the bad usage of the term Freeware, you are right. I didn't know the term of FOSS, the reason why I have used this inappropriate term.


After I have posted, I have tried to find an over way to read my file, until this code on Octave :


clear;
dlmread('1.txt',' ');
fid = [1,2,lines('400.txt'),5];


the .txt is read and I can see my value. Unfortunately, the text file doesn't present always the same amount of space between each value, giving this kind of table (example) :

x1     x2    x3     x4    x5    x6      x7      .... x
1        0      0     0      0     2        0           0        
2        0      0     0      3     0        0           0
3        0      0     0      0    36       0           0  
4        0      0     8      0     0        0           0    
5        0      0     0      0     0        0           5        
6        0      0     0      3     0        0           0                                                                

In the end, I want to do 2 column :
                                                                           
x1     x2
1        2        
2        3  
3       36  
4        8  
5        5        
6        3

to plot(x1,x2)

I have posted too quickly yesterday, my ask lack of explanation.

Cordially,

Guillaum
Reply | Threaded
Open this post in threaded view
|

Re: Matlab to Octave - problem wih the conversion

Juan Pablo Carbajal-2
On Thu, Aug 4, 2016 at 1:49 PM, Istan <[hidden email]> wrote:

> Hi,
>
> Thank you for your answer and these informations.
>
> I m a new user of matlab and octave. I m sorry for the bad usage of the term
> Freeware, you are right. I didn't know the term of FOSS, the reason why I
> have used this inappropriate term.
>
>
> After I have posted, I have tried to find an over way to read my file, until
> this code on Octave :
>
>
> clear;
> dlmread('1.txt',' ');
> fid = [1,2,lines('400.txt'),5];
>
>
> the .txt is read and I can see my value. Unfortunately, the text file
> doesn't present always the same amount of space between each value, giving
> this kind of table (example) :
>
> x1     x2    x3     x4    x5    x6      x7      .... x
> 1        0      0     0      0     2        0           0
> 2        0      0     0      3     0        0           0
> 3        0      0     0      0    36       0           0
> 4        0      0     8      0     0        0           0
> 5        0      0     0      0     0        0           5
> 6        0      0     0      3     0        0           0
>
> In the end, I want to do 2 column :
>
> x1     x2
> 1        2
> 2        3
> 3       36
> 4        8
> 5        5
> 6        3
>
> to plot(x1,x2)
>
> I have posted too quickly yesterday, my ask lack of explanation.
>
> Cordially,
>
> Guillaum
>
>
>
> --
> View this message in context: http://octave.1599824.n4.nabble.com/Matlab-to-Octave-problem-wih-the-conversion-tp4678981p4679002.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave

Lets say you have an example.txt file like what you showed in your
mail (only the 7 first columns).
The following code reads the data
ncols = 7;  # number of columns in file
nhead = 1; # number of header lines here: x1 x2 x3 x4 x5 x6 x7
data = txtread ("example.txt", "%d", "headerlines", nhead);
data = reshape (data, ncols, length(data) / ncols).'
ans =

   1   0   0   0   0   2   0
   2   0   0   0   3   0   0
   3   0   0   0   0  36   0
   4   0   0   8   0   0   0
   5   0   0   0   0   0   0
   6   0   0   0   3   0   0


then you can do

plot (data(:,1), data(:,2))

FOSS : https://en.wikipedia.org/wiki/Free_and_open-source_software

The Terms of Use of the "Matlab file central", which a place to
"share" the code, says in section 2.iii
(http://ch.mathworks.com/matlabcentral/termsofuse.html)

"Content that you submit must not directly compete with products
offered by MathWorks. Content submitted to File Exchange may only be
used with MathWorks products."

I would be surprised to read something similar in the terms of use (or
the user agreement) in the Matlab product or the function used to
generate the relevant code. Please do check and make no assumptions
about the rights you have on the code.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Matlab to Octave - problem wih the conversion

Juan Pablo Carbajal-2
On Thu, Aug 4, 2016 at 3:16 PM, Juan Pablo Carbajal
<[hidden email]> wrote:
> I would be surprised

I meant "I would not be surprised"

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Matlab to Octave - problem wih the conversion

Sebastian Schöps
In reply to this post by Juan Pablo Carbajal-2
Juan Pablo Carbajal-2 wrote
The Terms of Use of the "Matlab file central", which a place to
"share" the code, says in section 2.iii
(http://ch.mathworks.com/matlabcentral/termsofuse.html)

"Content that you submit must not directly compete with products
offered by MathWorks. Content submitted to File Exchange may only be
used with MathWorks products."

I would [not] be surprised to read something similar in the terms of use (or
the user agreement) in the Matlab product or the function used to
generate the relevant code. Please do check and make no assumptions
about the rights you have on the code.
Please don't make the situation of proprietary software worse than it is by assuming what Mathworks might or might not allow. The Matlab license agreement says in it's addendum:

"2. USER CREATED FILES.  This Addendum does not apply to MATLAB code files,
Simulink model files, MEX-files, MAT-files, VHDL-files, Verilog-files,
FIG-files and P-files that are created by Licensee and that do not include any
code obtained from MATLAB code files, Simulink model files, MAT-files, P-code,
C/C++ files, VHDL-files, Verilog-files, TLC-files, or other Source Code files
supplied with the Programs ("User Files").  Licensee may distribute or
sublicense without restriction, User Files provided that a principal purpose of
the distribution or sublicense is not to replace or replicate a Program or any
part of a Program."

see https://www.mathworks.com/help/javabuilder/MWArrayAPI/license_agreement.txt.

I am not a lawyer but that sounds quite relaxed to me.
Reply | Threaded
Open this post in threaded view
|

Re: Matlab to Octave - problem wih the conversion

Istan
In reply to this post by Juan Pablo Carbajal-2
I have tried this code :

clear;
ncols = 7;  # number of columns in file
nhead = 1; # number of header lines here: x1 x2 x3 x4 x5 x6 x7
data = txtread ("1.txt", "%d", "headerlines", nhead);
data = reshape (data, ncols, length(data) / ncols).'

It returns me this following error :

error: 'txtread' undefined near line 4 column 8
error: called from
    test3 at line 4 column 6


I do not want to take that risk, so can I canceled my first post without cancel the others ?
Reply | Threaded
Open this post in threaded view
|

Re: Matlab to Octave - problem wih the conversion

Juan Pablo Carbajal-2
In reply to this post by Sebastian Schöps
On Thu, Aug 4, 2016 at 3:10 PM, Sebastian Schöps <[hidden email]> wrote:

> Juan Pablo Carbajal-2 wrote
>> The Terms of Use of the "Matlab file central", which a place to
>> "share" the code, says in section 2.iii
>> (http://ch.mathworks.com/matlabcentral/termsofuse.html)
>>
>> "Content that you submit must not directly compete with products
>> offered by MathWorks. Content submitted to File Exchange may only be
>> used with MathWorks products."
>>
>> I would [not] be surprised to read something similar in the terms of use
>> (or
>> the user agreement) in the Matlab product or the function used to
>> generate the relevant code. Please do check and make no assumptions
>> about the rights you have on the code.
>
> Please don't make the situation of proprietary software worse than it is by
> assuming what Mathworks might or might not allow. The Matlab license
> agreement says in it's addendum:
>
I am not making assumptions, this exactly why I suggest to read the license.
I do not know from where the user got the file. Most of them get it
form Central, so I think my comment is relevant and I am not making
anything worse.
Also the text below doesn't apply to the files generated by matlab
programs, but to user created files, which wasn't the case here.

> "2. USER CREATED FILES.  This Addendum does not apply to MATLAB code files,
> Simulink model files, MEX-files, MAT-files, VHDL-files, Verilog-files,
> FIG-files and P-files that are created by Licensee and that do not include
> any
> code obtained from MATLAB code files, Simulink model files, MAT-files,
> P-code,
> C/C++ files, VHDL-files, Verilog-files, TLC-files, or other Source Code
> files
> supplied with the Programs ("User Files").  Licensee may distribute or
> sublicense without restriction, User Files provided that a principal purpose
> of
> the distribution or sublicense is not to replace or replicate a Program or
> any
> part of a Program."
>
> see
> https://www.mathworks.com/help/javabuilder/MWArrayAPI/license_agreement.txt.
>
> I am not a lawyer but that sounds quite relaxed to me.
>
>
>
> --
> View this message in context: http://octave.1599824.n4.nabble.com/Matlab-to-Octave-problem-wih-the-conversion-tp4678981p4679006.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Matlab to Octave - problem wih the conversion

Juan Pablo Carbajal-2
In reply to this post by Istan
On Thu, Aug 4, 2016 at 3:20 PM, Istan <[hidden email]> wrote:

> I have tried this code :
>
> clear;
> ncols = 7;  # number of columns in file
> nhead = 1; # number of header lines here: x1 x2 x3 x4 x5 x6 x7
> data = txtread ("1.txt", "%d", "headerlines", nhead);
> data = reshape (data, ncols, length(data) / ncols).'
>
> It returns me this following error :
>
> error: 'txtread' undefined near line 4 column 8
> error: called from
>     test3 at line 4 column 6
>
>
> I do not want to take that risk, so can I canceled my first post without
> cancel the others ?
>
>
>
> --
> View this message in context: http://octave.1599824.n4.nabble.com/Matlab-to-Octave-problem-wih-the-conversion-tp4678981p4679007.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave

Sorry, the function is "textread". Check

help textread

to know how to use it

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Matlab to Octave - problem wih the conversion

Istan
In reply to this post by Istan
It returns me a data as follow :

data =

     0
  1060
     0
  1060
     0
  1060
     0
  1061
     0
  1061
     0
  1062
     0
  1062
     0
  1063
     0
  1064
     0

Maybe something is wrong in my understanding of the file, that I upload here : 1.txt
Reply | Threaded
Open this post in threaded view
|

Re: Matlab to Octave - problem wih the conversion

Sebastian Schöps
In reply to this post by Juan Pablo Carbajal-2
Juan Pablo Carbajal-2 wrote
On Thu, Aug 4, 2016 at 3:10 PM, Sebastian Schöps <[hidden email]> wrote:
> Please don't make the situation of proprietary software worse than it is by
> assuming what Mathworks might or might not allow. The Matlab license
> agreement says in it's addendum:
>
Also the text below doesn't apply to the files generated by matlab
programs, but to user created files, which wasn't the case here.
Indeed, the question is now how do you define "user created". Nonetheless, I think our case is still included since even "auto generated" m-files are derived from user input. They mention fig-files explicitly which are not much different, they are automatically generated m-files. However, let's agree to disagree :)

Seb.
Reply | Threaded
Open this post in threaded view
|

Re: Matlab to Octave - problem wih the conversion

Juan Pablo Carbajal-2
In reply to this post by Istan
On Thu, Aug 4, 2016 at 3:33 PM, Istan <[hidden email]> wrote:

> It returns me a data as follow :
>
> data =
>
>      0
>   1060
>      0
>   1060
>      0
>   1060
>      0
>   1061
>      0
>   1061
>      0
>   1062
>      0
>   1062
>      0
>   1063
>      0
>   1064
>      0
>
> Maybe something is wrong in my understanding of the file, that I upload here
> :  1.txt <http://octave.1599824.n4.nabble.com/file/n4679010/1.txt>
>
>
>
>
> --
> View this message in context: http://octave.1599824.n4.nabble.com/Matlab-to-Octave-problem-wih-the-conversion-tp4678981p4679010.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave

Your file is already octave friendly (headers with "%" will be ignored
automatically). Just do
data = load("1.txt");

plot(data(:,1), data(:,2))

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Matlab to Octave - problem wih the conversion

Istan
It does'nt run in state. I have done few changes in the code and it is ok :


clear all;
ncols = 2;  # number of columns in file
nhead = 1; # number of header lines here: x1 x2 x3 x4 x5 x6 x7
data = textread ("1.txt", "%f", "headerlines", nhead);
data = reshape (data, ncols, length(data) / ncols)'
x =data(:,1);
y = data(:,2)
plot(x*1000,y)

Thank you for all !