IO package: Difference between revisions

1,770 bytes added ,  19 August 2014
Line 1: Line 1:
The IO package is part of the octave-forge project and provides input/output from/in external formats.
The IO package is part of the octave-forge project and provides input/output from/in external formats.


=== About read/write support (TL;DR) ===
=== About read/write support ===


Most people need this package to read and write Excel files. But the io package can read/write Open/Libre Office, Gnumeric and some less important files too.
Most people need this package to read and write Excel files. But the io package can read/write Open/Libre Office, Gnumeric and some less important files too.
Line 26: Line 26:
R : only read;  + : full read/write; ~ : dependent on Excel version
R : only read;  + : full read/write; ~ : dependent on Excel version
</nowiki></pre>
</nowiki></pre>
==== xlswrite / odswrite versus xlsopen / odsopen ..... xlsclose / odsclose ====
Matlab users are used to xlsread and xlswrite, functions that can only read data from, or write data to, one sheet in a spreadsheet file at a time. For each read, xlsread has to first read the entire spreadsheet file, for write operations xlswrite also has to finally write it out completely to disk.
For those of you who love this for OpenOffice/LibreOffice files, I've made odswrite and odsread who portray the same inefficiency.
If you want to move multiple pieces of data to/from a spreadsheet file, the io package offers a much more versatile scheme:
* First open the spreadsheet file using xlsopen (for Excel or gnumeric files) of odsopen (.ods or .gnumeric).
'''NOTE''': the output of these functions is a file pointer handle that you should treat carefully!
* (for reading data) Read the data using raw_data = xls2oct (fileptr [,sheet#] [,cellrange] [,options])
* Next, optionally split the data in numerical, text and raw data and optionally get the limits of where these came from:
[num, txt, raw, lims] = parsecell (data, <fileptr.lims>)
* (for writing data) Write the data using <fileptr> = oct2xls (data, <fileptr> [,sheet#] [,cellrange] [,options])
* When you're finished, DO NOT FORGET to colse the file pointer handle:
<fileptr> = xlsclose (<fileptr>)</pre>
Mixing read and write operations in any order is permitted (the only exception: not with the JXL -JExcelAPI- interface).
The same goes for odsopen-ods2oct-oct2ods-odsclose sequences.
Obviously this is much more flexible (and FASTER) than xlsread and xlswrite. In fact, Octave's io package xlsread is a mere wrapper for an xlsopen-xls2oct-parsecell-xlsclose sequence. Similarly for xlswrite, odsread, and odswrite.




Line 33: Line 54:
* .xls - is an outdated default binary file format from <= Office 2003 - '''try to avoid this format!'''
* .xls - is an outdated default binary file format from <= Office 2003 - '''try to avoid this format!'''
* .xlsx - is the new default file format since Office 2007. [https://en.wikipedia.org/wiki/OOXML It consists of xml files stored in a .zip container.] - '''always save in or convert to this format!'''
* .xlsx - is the new default file format since Office 2007. [https://en.wikipedia.org/wiki/OOXML It consists of xml files stored in a .zip container.] - '''always save in or convert to this format!'''
** The ''(new)'' OCT interface can read ''(since version 1.2.5)'' and write ''(since version 2.2.0)'' .xlsx files dependency-free! No need of MS Windows+Office nor Java.
* The ''(new)'' OCT interface can read ''(since version 1.2.5)'' and write ''(since version 2.2.0)'' .xlsx files dependency-free! No need of MS Windows+Office nor Java.
* Windows is notorious for hiding "known" file extensions. However in Windows Explorer it is easy to change this and have Windows show all file extensions.
* Windows is notorious for hiding "known" file extensions. However in Windows Explorer it is easy to change this and have Windows show all file extensions.


==== different interfaces ====
==== different interfaces ====
Line 56: Line 78:


For some rarely used file formats you'll need LibreOffice + Octave built with Java enabled + a Java JRE or -JDK. But OK, once there you can enjoy formats then like Unified Office Format, Data Interchange Format, SYLK, OpenDocument Flat XML, the old OpenOffice.org .sxc format and some others you may have heard of ;-)
For some rarely used file formats you'll need LibreOffice + Octave built with Java enabled + a Java JRE or -JDK. But OK, once there you can enjoy formats then like Unified Office Format, Data Interchange Format, SYLK, OpenDocument Flat XML, the old OpenOffice.org .sxc format and some others you may have heard of ;-)


==== force an interface ====
==== force an interface ====
Line 71: Line 94:


And so on ...
And so on ...


==== Java example ====
==== Java example ====
Anonymous user