bugGNU Octave - Bugs: bug #49389, inputParser changes order of...

 
 

bug #49389: inputParser changes order of Results structure depending on order by which parameters are assigned

Submitter:  Carlo de Falco <cdf>
Submitted:  Wed 19 Oct 2016 10:37:50 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Fri 14 Apr 2023 09:20:37 PM UTC, comment #12: 

I changed Octave to match Matlab and keep a sorted order for Parameters in Results field.  See bug #64003.

Marking this bug as Fixed and closing report.

Rik <rik5>
Group administrator
Sun 30 Oct 2016 03:36:53 PM UTC, comment #11: 

2016a:


>> ip = inputParser ();
ip.addParameter ('b', 2);
ip.addParameter ('a', 1);
ip.Parameters
ip.parse ('a', 2, 'b', 2);
ip.Results

ans =

    'a'    'b'


ans =

    a: 2
    b: 2


Carlo de Falco <cdf>
Group Member
Sun 30 Oct 2016 03:18:58 PM UTC, comment #10: 

With matlab 2015a


ans =

    'a'    'b'


ans =

    a: 2
    b: 2


Anonymous
Tue 25 Oct 2016 12:13:19 AM UTC, comment #9: 

For completeness, can this test be run under Matlab?


ip = inputParser ();
ip.addParameter ('b', 2);
ip.addParameter ('a', 1);
ip.Parameters
ip.parse ('a', 2, 'b', 2);
ip.Results


This checks that the Parameters field is being used to order the output in Results, and also whether the Parameters list is held in alphabetical order.

Rik <rik5>
Group administrator
Fri 21 Oct 2016 03:47:01 PM UTC, comment #8: 

What I am trying to achieve is something similar to the way graphics properties are handled.  Every object has a core set of  graphics properties that are listed first, followed by the list of properties that are specific to the object in question.

From a user perspective, I think it will be easiest to understand if the core set of ODE solver options is presented first and in alphabetical order, followed by any customized properties supplied by the user.

One way to achieve that is to have full Matlab compatibility for the inputParser function.  The other would be to post-process the Results structure with orderfields(), but it sounds like Carlo has an approach that will work.

Rik <rik5>
Group administrator
Fri 21 Oct 2016 02:30:53 PM UTC, comment #7: 

@Carnë

While trying to prepare a patch to avoid changing the order of parameters with "parse ()" I noticed that "Switches" are not available in Matlab, why do we have that in Octave? Is there
some backward compatibility we need to keep?


Carlo de Falco <cdf>
Group Member
Fri 21 Oct 2016 02:12:45 PM UTC, comment #6: 

@Rik

For completeness here's the result of the test you asked:


>> x.b = 2;
>> x.a = 1;
>> x

x =

    b: 2
    a: 1


Carlo de Falco <cdf>
Group Member
Fri 21 Oct 2016 02:07:12 PM UTC, comment #5: 

@Rik

Matlab does not keep struct keys sorted alphabetically
they are ordered according to the order in which they
were added to the struct.

Indeed the properties returned by odeset are not in
alphabetical order in Matlab either:


>> o = odeset ()

o =

              AbsTol: []
                 BDF: []
              Events: []
         InitialStep: []
            Jacobian: []
           JConstant: []
            JPattern: []
                Mass: []
        MassSingular: []
            MaxOrder: []
             MaxStep: []
         NonNegative: []
         NormControl: []
           OutputFcn: []
           OutputSel: []
              Refine: []
              RelTol: []
               Stats: []
          Vectorized: []
    MStateDependence: []
           MvPattern: []
        InitialSlope: []


on the other hand they do not change with each assignment.

@Carnë

In inputParser.parse () the values passed are assigned first,
then the the remaining Parameters are assigned the default value.

If we would assign the default value to all parameters first,
then overwrite those passed to parse we would get the same behaviour
as Matlab, and it would propbably also run a bit faster.

But you are right that this does not seem to be a documented Matlab
feature so we don't necessarily need to copy it.







Carlo de Falco <cdf>
Group Member
Fri 21 Oct 2016 11:24:22 AM UTC, comment #4: 


> It may be that Matlab always maintains struct keys in sorted order.


Matlab struct keys are ordered by insertion. But Results does not have values inserted until parse is called. This is the same in Matlab addParameter/addParamValue


>> p = inputParser ();
>> p.addParamValue ('a', 1);
>> p.addParamValue ('b', 2);
>> p.Results

ans =

1x1 struct array with no fields.


The actual order of insertion is kept, in Parameters:


## Matlab
>> p.Parameters

ans =

    'a'    'b'
## Octave
octave-gui:4> p.Parameters
ans =
{
  [1,1] = a
  [1,2] = b
}


If Matlab Results has the same order of values as the order of add*, then that's an implementation detail. And an undocumented one. Since Results is a struct, the type of thing where order of keys does not usually matter, what is the use case for this?

Also, is this a Matlab compatibility issue? There are more functions in Octave that return structs where the order of keys differ from Matlab.

Carnë Draug <carandraug>
Group Member
Fri 21 Oct 2016 04:02:26 AM UTC, comment #3: 

To see whether Matlab internally orders struct fields try


x.b = 2;
x.a = 1;
x


It may be that Matlab always maintains struct keys in sorted order.

Rik <rik5>
Group administrator
Thu 20 Oct 2016 07:48:05 PM UTC, comment #2: 

inputParser does not change the order of Results because Results is empty before parse.

Is this bug really about the order of values in Results (a scalar struct) or about the display method?

Carnë Draug <carandraug>
Group Member
Thu 20 Oct 2016 03:22:56 PM UTC, comment #1: 

@Carlo: Could you run one more test in Matlab to see if it always sorts the return values by Parameter name, or whether it uses the order in which parameters were added to the inputParser.


ip = inputParser ();
ip.addParameter ('b', 2);
ip.addParameter ('a', 1);
ip.parse ('a', 2, 'b', 2);
ip.Results



Rik <rik5>
Group administrator
Wed 19 Oct 2016 10:37:50 PM UTC, original submission:  

The following example demonstrates the issue

In Matlab 2016a:

%-- Matlab test --
>> ip = inputParser ();
>> ip.addParameter ('a', 1);
>> ip.addParameter ('b', 2);
>> ip.parse ('b', 2, 'a', 2);
>> ip.Results

ans =

   a: 2
   b: 2
%-- end Matlab test --


In Octave 4.2-rc2:

%-- Octave test --
ip = inputParser ();
ip.addParameter ('a', 1);
ip.addParameter ('b', 2);
ip.parse ('b', 2, 'a', 2)
ip.Results
ans =

 scalar structure containing the fields:

   b =  2
   a =  2
%-- end Octave test --





Carlo de Falco <cdf>
Group Member

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by cdf (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-04-14 rik5 StatusConfirmed Fixed
        Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code