help-octave
[Top][All Lists]
Advanced

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

Re: how to save matrix from an oct file


From: Jaroslav Hajek
Subject: Re: how to save matrix from an oct file
Date: Wed, 12 May 2010 08:18:44 +0200

On Wed, May 12, 2010 at 8:04 AM, Carlo de Falco <address@hidden> wrote:
>
> On 11 May 2010, at 12:54, Carlo de Falco wrote:
>
>> Hi,
>> for debugging an oct file I am working on, I would like to save a
>> SparseMatrix to a file so that I can later load it in octave and
>> inspect it.
>>
>> I naively tried the following:
>>
>> 1) for ascii format
>>
>> SparseMatrix mat;
>>
>> ....
>>
>> std::ofstream os ("mat.txt");
>> octave_value (mat).save_ascii (os);
>> os.close();
>>
>> which seems to work but saves the matrix entries in IJV format and I
>> have to later convert them with spconvert.
>>
>> 2) for binary format
>>
>> std::ofstream os ("mat.mat");
>> bool boolvar(false);
>> octave_value (mat).save_binary (os, boolvar);
>> os.close();
>>
>> which doesn't seem to work correctly as
>>
>> >> load mat.mat
>> error: load: mat.mat: inconsistent number of columns near line 2
>> error: load: unable to extract matrix size from file `mat.mat'
>>
>> What is the correct way to do this?
>> Thanks in advance,
>> c.
>
>
> To (partly) respond to my own question, the following seems to work in
> 3.2.3:
>
> #include <octave/ls-oct-ascii.h>
> SparseMatrix mat;
> ...
> std::ofstream os ("mat.mat");
> bool boolvar(false);
> save_ascii_data (os, octave_value (mat),
>                  std::string("mat"),
>                  boolvar, boolvar);
> os.close();
>
> But the equivalent for binary format still fails:
>
> #include <octave/ls-oct-binary.h>
> SparseMatrix mat;
> ...
> std::ofstream os ("mat.mat");
> bool boolvar(false);
> save_binary_data (os, octave_value (mat),
>                   std::string("mat"), std::string(""),
>                   boolvar, boolvar);
> os.close();
>
>  >> load mat.mat
> error: load: mat.mat: inconsistent number of columns near line 2
> error: load: unable to extract matrix size from file `mat.mat'
>


try adding

#include <octave/load-save.h>

...
write_header (os, LS_BINARY);

apparently the ascii format will cope with a missing header, but it's
no surprise that the binary format doesn't.

-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

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