loading empty integer matrix failes when saved as "-text"

Jaroslav Hajek highegg at gmail.com
Fri Mar 20 05:37:54 CDT 2009


On Fri, Mar 20, 2009 at 11:26 AM, Benjamin Lindner <lindnerben at gmx.net> wrote:
> Jaroslav Hajek wrote:
>>
>> 2009/3/18 Benjamin Lindner <lindnerben at gmx.net>:
>>>
>>> Hello,
>>>
>>> Trying to load an empty integer matrix saved in "-text" format fails.both
>>> on
>>> 3.0.2 and current development sources.
>>>
>>>> a=ones(0,1,"int8")
>>>> save("-text","t:\\test.mat","a");
>>>> clear a
>>>> load("-text","t:\\test.mat");
>>>
>>> error: load: failed to load matrix constant
>>> error: load: trouble reading ascii file `t:\test.mat'
>>> error: load: reading file t:\test.mat
>>>
>>> The problem is, that load_ascii() attempts to read data from the saved
>>> file
>>> even if the matrix is empty (and hence no data was saved in the first
>>> place).
>>>
>>> The attached changeset fixes the issue for the deveopment sources
>>>
>>> benjamin
>>>
>>>
>>> # HG changeset patch
>>> # User Benjamin Lindner <lindnerb at users.sourceforge.net>
>>> # Date 1237386193 -3600
>>> # Node ID e5cd8a6cd980089842bdca98d02d0f1efaa5f86d
>>> # Parent  03da3944075acd8478ac3a1dd8ea75b726425521
>>> attempt to read data only if saved matrix has non-zero size
>>>
>>> diff -r 03da3944075a -r e5cd8a6cd980 src/ChangeLog
>>> --- a/src/ChangeLog     Wed Mar 18 09:48:37 2009 +0100
>>> +++ b/src/ChangeLog     Wed Mar 18 15:23:13 2009 +0100
>>> @@ -1,3 +1,8 @@
>>> +2009-03-18  Benjamin Lindner <lindnerb at users.sourceforge.net>
>>> +
>>> +       * ov-base-int.cc (load_ascii): Attempt to read data from file
>>> only
>>> +       if saved matrix is non-empty
>>> +
>>>  2009-03-15  Jaroslav Hajek  <highegg at gmail.com>
>>>
>>>       * DLD-FUNCTIONS/cellfun.cc (Fcellslices): New DLD function.
>>> diff -r 03da3944075a -r e5cd8a6cd980 src/ov-base-int.cc
>>> --- a/src/ov-base-int.cc        Wed Mar 18 09:48:37 2009 +0100
>>> +++ b/src/ov-base-int.cc        Wed Mar 18 15:23:13 2009 +0100
>>> @@ -212,12 +212,16 @@
>>>
>>>         T tmp(dv);
>>>
>>> -         is >> tmp;
>>> +         if (!dv.any_zero ())
>>> +           {
>>>
>>> -         if (!is)
>>> -           {
>>> -             error ("load: failed to load matrix constant");
>>> -             success = false;
>>> +             is >> tmp;
>>> +
>>> +             if (!is)
>>> +               {
>>> +                 error ("load: failed to load matrix constant");
>>> +                 success = false;
>>> +               }
>>>           }
>>>
>>>         this->matrix = tmp;
>>>
>>
>> I personally don't understand why the stream input operators for
>> NDArrays (including intNDArray) disallow reading an empty matrix. Does
>> anyone have an explanation? Otherwise, I think we should just fix
>> those.
>
> Well, operator>> in intNDArray.cc checks if nelem() is less than 1 and if
> so, sets ios::badbit.
> Since an empty matrix has nelem()==0, reading from the input stream will set
> ios::badbit, and thus load_ascii() fails.
>
> If you ask why this is so, I'm at a loss too.
>
> So either, operator>> should tolerate empty matrices, or operator>> should
> not be called to read empty matrices.
>
> I cannot tell which is the better solution. I went for the latter.

OK, that was the easier, but I think the former is the better. I don't
see a reason for this.

> But I can try to fix operator>> and post it, if this is preferable.
>
> benjamin
>
>

I just pushed a patch:
http://hg.savannah.gnu.org/hgweb/octave/rev/dc07bc4157b8

cheers

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



More information about the Bug-octave mailing list