bugGNU Octave - Bugs: bug #63160, Octave unable to iterate over...

 
 

bug #63160: Octave unable to iterate over object arrays in a for-loop

Submitter:  Guillaume <gyom>
Submitted:  Wed 05 Oct 2022 11:43:39 AM UTC
   
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Unexpected Error or Warning
Status:  Confirmed Assigned to:  None
Originator Name:  Guillaume Open/Closed:  * Open
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Wed 05 Oct 2022 06:51:58 PM UTC, comment #6: 

Yes, Octave should iterate over all "array" objects (numeric, cell, struct, class object) in a consistent way.  It's made a little bit more complex in Octave because we have "octave_value" objects that are containers for the other things instead of just having the octave_value object contain the dimensions and data directly as I assume Matlab does with the mxArray thing.

John W. Eaton <jwe>
Group administrator
Wed 05 Oct 2022 03:51:23 PM UTC, comment #5: 

sorry, our posts crossed @arun.  structure array indexing has generally always worked, there's even a special syntax for it.  in my test the object is a 1x2 row vector array, so that's not the issue.  from the error, it appears to be throwing a specific type checking error.  curious if the error just artificially relaxed if the for loop code would choke on other types of objects (and is classdef different than non classdef objects)

updating title for a bit more clarity.

Nicholas Jankowski <nrjank>
Group Member
Wed 05 Oct 2022 03:46:11 PM UTC, comment #4: 

interesting. octave has always been able to iterate over a char array, since that's handled the same as a numeric array:


>> for idx = "abcd", idx, endfor
idx = a
idx = b
idx = c
idx = d


similar for cell arrays, and structs.  At first glance I was assuming this is a unique feature of "new" string objects.  But a quick test shows matlab can iterate over other object types as well.

trying a generic class object:


classdef myclass
  properties
    prop;
  end
  methods
    function obj = myclass(val)
      obj.prop = val;
    end
    function val = myprop(obj)
      val =[obj.prop];
    end
  end
end


you can step through the object array in matlab but not octave:

matlab 2022a:

a(1) = myclass(1);
a(2) = myclass(2);

for idx = a, idx, end

idx =
  myclass with properties:
    prop: 1

idx =
  myclass with properties:
    prop: 2


trying the same in octave 8.0.0 (hg id: 8b75954a4670)


>> for idx = a, idx, end
error: invalid type in for loop expression near line 1, column 1


based on the error message i don't think this is just related to bug #47755, as octave still doesn't consistently handle classdef object arrays in the first place.

Nicholas Jankowski <nrjank>
Group Member
Wed 05 Oct 2022 03:44:05 PM UTC, comment #3: 

I can do that in Octave as long as it's a row vector:


>> A
A =
  scalar structure containing the fields:
    foo = 1
    bar = 2

>> B
B =
  scalar structure containing the fields:
    foo = 3
    bar = 4

>> for i = [A, B], i, end
i =
  scalar structure containing the fields:
    foo = 1
    bar = 2

i =
  scalar structure containing the fields:
    foo = 3
    bar = 4


Does it help to force-convert to a row vector like this?


for i = objects(:).'  # convert to row vector
  i
end


Arun Giridhar <arungiridhar>
Group Member
Wed 05 Oct 2022 03:04:45 PM UTC, comment #2: 

I came across this from one of the examples here that is iterating over a string array:
https://blogs.mathworks.com/matlab/2022/09/15/an-introduction-to-dictionaries-associative-arrays-in-matlab/


for word = words'


Guillaume <gyom>
Wed 05 Oct 2022 02:22:23 PM UTC, comment #1: 

What type of object are you trying to index with? checking for compatibily, according to the matlab help, with the "for index = valArray" format, "the input valArray can be of any MATLAB data type, including a character vector, cell array, or struct."  Octave's help isn't this explicit, just saying that in "for var = expression", "expression" is any valid expression. Does anyone know for certain if it is supposed to work for an arbitrary object array?

Nicholas Jankowski <nrjank>
Group Member
Wed 05 Oct 2022 11:43:39 AM UTC, original submission:  

It seems that it is not possible to iterate over the elements of a object array in a for-loop:


>> for i = obj
  i
endfor
error: invalid type in for loop expression near line 1, column 1


Guillaume <gyom>

 

(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 jwe (Posted a comment)
  • -email is unavailable- added by arungiridhar (Posted a comment)
  • -email is unavailable- added by nrjank (Posted a comment)
  • -email is unavailable- added by gyom (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 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2022-10-05 nrjank SummaryIterating over objects in a for-loop Octave unable to iterate over object arrays in a for-loop
    2022-10-05 nrjank StatusNeed Info Confirmed
    2022-10-05 nrjank StatusNone Need Info

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code