bug 3593
Allan CORNET [Fri, 13 Nov 2009 13:08:59 +0000 (14:08 +0100)]
12 files changed:
scilab/CHANGES_5.2.X
scilab/modules/spreadsheet/help/en_US/excel2sci.xml
scilab/modules/spreadsheet/help/en_US/read_csv.xml [new file with mode: 0644]
scilab/modules/spreadsheet/help/en_US/write_csv.xml [moved from scilab/modules/spreadsheet/help/pt_BR/excel2sci.xml with 50% similarity]
scilab/modules/spreadsheet/macros/excel2sci.sci
scilab/modules/spreadsheet/macros/read_csv.sci [new file with mode: 0644]
scilab/modules/spreadsheet/macros/sci2excel.sci
scilab/modules/spreadsheet/macros/write_csv.sci [new file with mode: 0644]
scilab/modules/spreadsheet/tests/unit_tests/readwrite_csv.dia.ref [new file with mode: 0644]
scilab/modules/spreadsheet/tests/unit_tests/readwrite_csv.tst [new file with mode: 0644]
scilab/modules/spreadsheet/tests/unit_tests/sci2excel.dia.ref
scilab/modules/spreadsheet/tests/unit_tests/sci2excel.tst

index 3e38dae..896c04f 100644 (file)
@@ -40,6 +40,8 @@ Compilation:
 Bug fixes:
 ==========
 
+* bug 3593 fixed - macros to read and write csv file format updated.
+
 * bug 4401 fixed - isnum returned wrong values.
 
 * bug 4565 fixed - getvalue macro is definied two times.
index 13df3f4..3e2b134 100644 (file)
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="UTF-8"?>
+<?xml version="1.0" encoding="ISO-8859-1"?>
 <!--
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  * Copyright (C) ????-2008 - INRIA
  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  *
  -->
-<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" version="5.0-subset Scilab" xml:lang="en" xml:id="excel2sci">
-  <info>
-    <pubdate>$LastChangedDate$</pubdate>
-  </info>
+<refentry version="5.0-subset Scilab" xml:id="excel2sci" xml:lang="en"
+          xmlns="http://docbook.org/ns/docbook"
+          xmlns:xlink="http://www.w3.org/1999/xlink"
+          xmlns:svg="http://www.w3.org/2000/svg"
+          xmlns:ns4="http://www.w3.org/1999/xhtml"
+          xmlns:mml="http://www.w3.org/1998/Math/MathML"
+          xmlns:db="http://docbook.org/ns/docbook">
+          
   <refnamediv>
     <refname>excel2sci</refname>
-    <refpurpose> reads ascii Excel files</refpurpose>
+    <refpurpose>obsolete see read_csv</refpurpose>
   </refnamediv>
-  <refsynopsisdiv>
-    <title>Calling Sequence</title>
-    <synopsis>M=excel2sci(fname [,sep])</synopsis>
-  </refsynopsisdiv>
-  <refsection>
-    <title>Parameters</title>
-    <variablelist>
-      <varlistentry>
-        <term>fname</term>
-        <listitem>
-          <para>character string. The file path</para>
-        </listitem>
-      </varlistentry>
-      <varlistentry>
-        <term>sep</term>
-        <listitem>
-          <para>character string. Excel separator used, default value is ","</para>
-        </listitem>
-      </varlistentry>
-      <varlistentry>
-        <term>M</term>
-        <listitem>
-          <para>matrix of strings</para>
-        </listitem>
-      </varlistentry>
-    </variablelist>
-  </refsection>
-  <refsection>
-    <title>Description</title>
-    <para>
-    Given an ascii  file created by Excel using "Text and comma" format
-    <literal>excel2sci(fname)</literal> returns the corresponding Scilab matrix of
-    strings. Use <literal>excel2sci(fname,sep )</literal> for an other choice of
-    separator.</para>
-    <para>
-    Note: You may eval all or part ot <literal>M</literal> using function <literal>evstr</literal>.</para>
-  </refsection>
+  
   <refsection>
     <title>See Also</title>
     <simplelist type="inline">
       <member>
-        <link linkend="read">read</link>
+        <link linkend="read_csv">read_csv</link>
       </member>
       <member>
-        <link linkend="evstr">evstr</link>
+        <link linkend="write_csv">write_csv</link>
       </member>
     </simplelist>
   </refsection>
diff --git a/scilab/modules/spreadsheet/help/en_US/read_csv.xml b/scilab/modules/spreadsheet/help/en_US/read_csv.xml
new file mode 100644 (file)
index 0000000..d73eb87
--- /dev/null
@@ -0,0 +1,115 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2009 - DIGITEO - Allan CORNET
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ -->
+<refentry version="5.0-subset Scilab" xml:id="read_csv" xml:lang="en"
+          xmlns="http://docbook.org/ns/docbook"
+          xmlns:xlink="http://www.w3.org/1999/xlink"
+          xmlns:svg="http://www.w3.org/2000/svg"
+          xmlns:ns4="http://www.w3.org/1999/xhtml"
+          xmlns:mml="http://www.w3.org/1998/Math/MathML"
+          xmlns:db="http://docbook.org/ns/docbook">
+  <info>
+    <pubdate>$LastChangedDate$</pubdate>
+  </info>
+
+  <refnamediv>
+    <refname>read_csv</refname>
+
+    <refpurpose>Read comma-separated value file</refpurpose>
+  </refnamediv>
+
+  <refsynopsisdiv>
+    <title>Calling Sequence</title>
+
+    <synopsis>M = read_csv(fname [,sep])</synopsis>
+  </refsynopsisdiv>
+
+  <refsection>
+    <title>Parameters</title>
+
+    <variablelist>
+      <varlistentry>
+        <term>fname</term>
+
+        <listitem>
+          <para>a character string. The file path.</para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term>sep</term>
+
+        <listitem>
+          <para>a character string. Field separator used, default value is
+          ","</para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term>M</term>
+
+        <listitem>
+          <para>a matrix of strings.</para>
+        </listitem>
+      </varlistentry>
+    </variablelist>
+  </refsection>
+
+  <refsection>
+    <title>Description</title>
+
+    <para>Given an ascii file with delimited fields, for instance created by a
+    spreadsheet using "Text and comma" format, read_csv(fname) returns the
+    corresponding Scilab matrix of strings. Use read_csv(fname,sep) for
+    another choice of separator.</para>
+
+    <para>Note: You may eval all or part of M using function evstr in order to
+    convert string variables into numeric variables.</para>
+  </refsection>
+
+  <refsection>
+    <title>Examples</title>
+
+    <programlisting role="example">// create a file with some data separated with tab    
+A = 1:50;
+mputl(strcat(string(A),ascii(9)), TMPDIR + '/foo.csv');
+
+// read csv file
+B = read_csv(TMPDIR + '/foo.csv');
+
+// eval B
+C = evstr(B);
+
+// compares original data and result
+and(A == C)</programlisting>
+  </refsection>
+
+  <refsection>
+    <title>See Also</title>
+
+    <simplelist type="inline">
+      <member><link linkend="write_csv">write_csv</link></member>
+
+      <member><link linkend="evstr">evstr</link></member>
+    </simplelist>
+  </refsection>
+
+  <refsection>
+    <title>Authors</title>
+
+    <variablelist>
+      <varlistentry>
+        <term>Allan CORNET</term>
+      </varlistentry>
+    </variablelist>
+  </refsection>
+</refentry>
@@ -1,7 +1,7 @@
-<?xml version="1.0" encoding="ISO-8859-1"?>
+<?xml version="1.0" encoding="UTF-8"?>
 <!--
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
- * Copyright (C) ????-2008 - INRIA
+ * Copyright (C) 2009 - DIGITEO - Allan CORNET
  *
  * This file must be used under the terms of the CeCILL.
  * This source file is licensed as described in the file COPYING, which
@@ -10,7 +10,7 @@
  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  *
  -->
-<refentry version="5.0-subset Scilab" xml:id="excel2sci" xml:lang="en"
+<refentry version="5.0-subset Scilab" xml:id="write_csv" xml:lang="en"
           xmlns="http://docbook.org/ns/docbook"
           xmlns:xlink="http://www.w3.org/1999/xlink"
           xmlns:svg="http://www.w3.org/2000/svg"
   </info>
 
   <refnamediv>
-    <refname>excel2sci</refname>
+    <refname>write_csv</refname>
 
-    <refpurpose>lê arquivos ASCII Excel</refpurpose>
+    <refpurpose>Read comma-separated value file</refpurpose>
   </refnamediv>
 
   <refsynopsisdiv>
-    <title>Seqüência de Chamamento</title>
+    <title>Calling Sequence</title>
 
-    <synopsis>M=excel2sci(fname [,sep])</synopsis>
+    <synopsis>write_csv(M, filename)</synopsis>
   </refsynopsisdiv>
 
   <refsection>
-    <title>Parâmetros</title>
+    <title>Parameters</title>
 
     <variablelist>
       <varlistentry>
-        <term>fname</term>
+        <term>filename</term>
 
         <listitem>
-          <para>string, o endereço do arquivo</para>
-        </listitem>
-      </varlistentry>
-
-      <varlistentry>
-        <term>sep</term>
-
-        <listitem>
-          <para>string. separador Excel utilizado, o valor padrão é ","</para>
+          <para>a character string. The file path.</para>
         </listitem>
       </varlistentry>
 
         <term>M</term>
 
         <listitem>
-          <para>matriz de strings</para>
+          <para>a matrix of strings.</para>
         </listitem>
       </varlistentry>
     </variablelist>
   </refsection>
 
   <refsection>
-    <title>Descrição</title>
+    <title>Description</title>
+
+    <para>write_csv(M, filename) writes matrix M into filename as
+    comma-separated values. The filename input is a string.</para>
+  </refsection>
+
+  <refsection>
+    <title>Examples</title>
 
-    <para>Dado um arquivo ASCII criado pelo Excel utilizando-se o formato
-    "Texto e vírgula" ("Text and comma") <literal>excel2sci(fname)</literal>
-    retorna a matriz de strings Scilab correspondente. Use
-    <literal>excel2sci(fname,sep )</literal> para uma outra escolha do
-    separador.</para>
+    <programlisting role="example">// save a matrix as csv file format
+A = [1:10] * 0.1;
+write_csv(A, TMPDIR + '/datas.csv');
 
-    <para>Nota: você pode avaliar <literal>M</literal> completa ou
-    parcialmente utilizando a função <literal>evstr</literal>.</para>
+// read as text
+mgetl(TMPDIR + '/datas.csv')
+
+r = read_csv(TMPDIR + '/datas.csv',ascii(9));
+r = strsubst(r,',','.');
+evstr(r)</programlisting>
   </refsection>
 
   <refsection>
-    <title>Ver Também</title>
+    <title>See Also</title>
 
     <simplelist type="inline">
-      <member><link linkend="read">read</link></member>
+      <member><link linkend="read_csv">read_csv</link></member>
 
       <member><link linkend="evstr">evstr</link></member>
+
+      <member><link linkend="mgetl">mgetl</link></member>
     </simplelist>
   </refsection>
+
+  <refsection>
+    <title>Authors</title>
+
+    <variablelist>
+      <varlistentry>
+        <term>Allan CORNET</term>
+      </varlistentry>
+    </variablelist>
+  </refsection>
 </refentry>
index 876ed9e..f5ddcde 100644 (file)
@@ -8,30 +8,12 @@
 // are also available at
 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 
-function mat=excel2sci(fname,sep)
-// Given an ascii  file created by Excel using "Text and comma" format
-// exel2sci(fname) returns the corresponding Scilab matrix of strings.
-
-if argn(2)<2 then sep=',',end
-v=mgetl(fname);
-ns=length(sep);
-mat=[];ki=1;
-for i=1:size(v,'*')
-  line=v(i)
-  K=[strindex(line,sep)];
-  ki=1;row=[];
-  for k=K
-    row=[row,part(line,ki:k-1)];
-    ki=k+ns;
-  end
-  row=[row,part(line,ki:length(line))]
-  if i>1 then
-    if size(row,2)>size(mat,2) then
-      mat($,size(row,2))=''
-    elseif size(row,2)<size(mat,2) then
-      row(size(mat,2))=''
-    end
+function mat = excel2sci(fname, sep)
+  warnobsolete("read_csv", "5.3");
+  
+  if argn(2)<2 then 
+    sep=',';
   end
-  mat=[mat;row];
-end
+  
+  mat = read_csv(fname, sep);
 endfunction
diff --git a/scilab/modules/spreadsheet/macros/read_csv.sci b/scilab/modules/spreadsheet/macros/read_csv.sci
new file mode 100644 (file)
index 0000000..91f2611
--- /dev/null
@@ -0,0 +1,55 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2008 - INRIA
+// Copyright (C) 2009 - DIGITEO - Allan CORNET
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+
+function mat = read_csv(fname,sep)
+// Given an ascii  file created by Excel using "Text and comma" format
+// read_csv(fname) returns the corresponding Scilab matrix of strings.
+
+  if (type(fname) <> 10) then
+    error(msprintf(gettext("%s: Wrong type for input argument #%d: a string expected.\n"), 'write_csv', 1));
+  end
+  
+  if (size(fname,'*')<> 1) then
+    error(msprintf(gettext("%s: Wrong size for input argument #%d: a string expected.\n"), 'write_csv', 1));  
+  end
+  
+  if argn(2)<2 then 
+    sep = ',';
+  end
+  
+  if ~isfile(fname) then
+    error(msprintf(gettext("%s: file %s does not exist.\n"), 'write_csv', 1, fname));  
+  end
+  
+  v = mgetl(fname);
+  ns = length(sep);
+  mat = [];
+  ki = 1;
+  for i = 1 : size(v,'*')
+    line = v(i);
+    K = [strindex(line, sep)];
+    ki = 1;
+    row = [];
+    for k = K
+      row = [row, part(line,ki:k-1)];
+      ki = k + ns;
+    end
+    row = [row, part(line, ki:length(line))];
+    if (i > 1) then
+      if size(row,2) > size(mat,2) then
+        mat($,size(row,2)) = '';
+      elseif size(row,2) < size(mat,2) then
+        row(size(mat,2)) = '';
+      end
+    end
+    mat = [mat; row];
+  end
+endfunction
index 6a3edec..ad8038e 100644 (file)
@@ -8,28 +8,16 @@
 // are also available at
 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 
-function sci2excel(var,fname,sep,dec)
-// var : a matrix of numbers or a matrix of strings
-// fname : a character string, the path of the file to create
-// sep   : the column separator mark, a tab by default
-// dec   : the decimal mark . or ,
-// Example
-// a=rand(3,5);
-// sci2spreadsheet(a,'/tmp/foo.txt',sep=',')
-if exists('sep','local')==0 then sep=code2str(-40),end
-if exists('dec','local')==0 then dec=',',end
-
-if dec<>['.',','] then 
-  error(msprintf(gettext("%s: Wrong input argument #%d: ''%s'' or ''%s'' expected.\n"), 'sci2excel', 3, '.', ','));
-end
-
-if type(var)==1 then
-  var=string(var)
-  if dec<>'.' then var =strsubst(var,'.',','),end
-end
-t=var(:,1)
-for k=2:size(var,2)
-  t=t+sep+var(:,k)
-end
-mputl(t,fname);
+function sci2excel(var, fname, sep, dec)
+  warnobsolete("write_csv", "5.3");
+  
+  if exists('sep','local') == 0 then
+    sep = ascii(9);
+  end
+  
+  if exists('dec','local') == 0 then
+    dec = ',';
+  end
+  
+  write_csv(var, fname, sep, dec);
 endfunction
diff --git a/scilab/modules/spreadsheet/macros/write_csv.sci b/scilab/modules/spreadsheet/macros/write_csv.sci
new file mode 100644 (file)
index 0000000..103d0a2
--- /dev/null
@@ -0,0 +1,67 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2008 - INRIA
+// Copyright (C) 2009 - DIGITEO - Allan CORNET
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+
+function write_csv(var, fname, sep, dec)
+// var : a matrix of numbers or a matrix of strings
+// fname : a character string, the path of the file to create
+// sep   : the column separator mark, a tab by default
+// dec   : the decimal mark . or ,
+// Example
+// a = rand(3,5);
+// write_csv(a,'/tmp/foo.txt',sep=',')
+
+  if exists('sep','local') == 0 then
+    sep = ascii(9);
+  end
+  
+  if exists('dec','local') == 0 then
+    dec = ',';
+  end
+  
+  if ( (type(var) <> 10) & (type(var) <> 1) ) then
+    error(msprintf(gettext("%s: Wrong type for input argument #%d: ''%s'' or ''%s'' expected.\n"), 'write_csv', 1, 'real', 'string'));
+  end
+
+  if (type(fname) <> 10) then
+    error(msprintf(gettext("%s: Wrong type for input argument #%d: a string expected.\n"), 'write_csv', 2));
+  end
+  
+  if (size(fname,'*')<> 1) then
+    error(msprintf(gettext("%s: Wrong size for input argument #%d: a string expected.\n"), 'write_csv', 2));  
+  end
+
+  if (type(dec) <> 10) then
+    error(msprintf(gettext("%s: Wrong type for input argument #%d: a string expected.\n"), 'write_csv', 3));
+  end
+
+  if (size(dec,'*')<> 1) then
+    error(msprintf(gettext("%s: Wrong size for input argument #%d: a string expected.\n"), 'write_csv', 3));  
+  end
+
+  if dec <> ['.',','] then 
+    error(msprintf(gettext("%s: Wrong value for input argument #%d: ''%s'' or ''%s'' expected.\n"), 'write_csv', 3, '.', ','));
+  end
+
+  if type(var) == 1 then
+    var = string(var);
+    if dec <> '.' then 
+      var = strsubst(var,'.',',');
+    end
+  end
+
+  t = var(:, 1);
+  for k=2:size(var, 2)
+    t = t + sep + var(:, k);
+  end
+  
+  mputl(t,fname);
+  
+endfunction
diff --git a/scilab/modules/spreadsheet/tests/unit_tests/readwrite_csv.dia.ref b/scilab/modules/spreadsheet/tests/unit_tests/readwrite_csv.dia.ref
new file mode 100644 (file)
index 0000000..7ce29e3
--- /dev/null
@@ -0,0 +1,14 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2008 - DIGITEO - Allan CORNET
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+ref1 = [ 0 1 2 3 4 5 6 7 8 9 ;
+        10 11 12 13 14 15 16 17 18 19 ;
+        20 21 22 23 24 25 26 27 28 29 ;
+        30 31 32 33 34 35 36 37 38 39 ;
+        40 41 42 43 44 45 46 47 48 49 ];
+write_csv(ref1, TMPDIR + '/test.csv');
+res = read_csv( TMPDIR + '/test.csv');
+if and(evstr(res) <> ref1) then bugmes();quit;end
diff --git a/scilab/modules/spreadsheet/tests/unit_tests/readwrite_csv.tst b/scilab/modules/spreadsheet/tests/unit_tests/readwrite_csv.tst
new file mode 100644 (file)
index 0000000..035a8a9
--- /dev/null
@@ -0,0 +1,17 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2008 - DIGITEO - Allan CORNET
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+
+ref1 = [ 0 1 2 3 4 5 6 7 8 9 ;
+        10 11 12 13 14 15 16 17 18 19 ;
+        20 21 22 23 24 25 26 27 28 29 ;
+        30 31 32 33 34 35 36 37 38 39 ;
+        40 41 42 43 44 45 46 47 48 49 ];
+
+write_csv(ref1, TMPDIR + '/test.csv');
+res = read_csv( TMPDIR + '/test.csv');
+
+if and(evstr(res) <> ref1) then pause,end
\ No newline at end of file
index 9c9de81..4d4eba2 100644 (file)
@@ -4,6 +4,7 @@
 //
 //  This file is distributed under the same license as the Scilab package.
 // =============================================================================
+warning('off');
 ref1 = [ 0 1 2 3 4 5 6 7 8 9 ;
         10 11 12 13 14 15 16 17 18 19 ;
         20 21 22 23 24 25 26 27 28 29 ;
index 55300e7..2a48203 100644 (file)
@@ -5,6 +5,7 @@
 //  This file is distributed under the same license as the Scilab package.
 // =============================================================================
 
+warning('off');
 ref1 = [ 0 1 2 3 4 5 6 7 8 9 ;
         10 11 12 13 14 15 16 17 18 19 ;
         20 21 22 23 24 25 26 27 28 29 ;