Add a New Comment (Rich Markup)
Comment Type & Canned Response: None None > Multiple Canned Responses Fixed in development Crash with no stack trace Already fixed in newer version Fixed in stable Bad description Bad description and crash Bad stack trace Obsolete version Duplicate and not fixed Duplicate and needs more info Duplicate and fixed Need info and old
( Jump to the original submission )
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.
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.
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.
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
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'
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?
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
(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
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 project members can vote.
Please enter the title of George Orwell's famous dystopian book (it's a date):
Follow 3 latest changes.
Copyright © 2023 Free Software Foundation, Inc. Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. The Levitating, Meditating, Flute-playing Gnu logo is a GNU GPL'ed image provided by the Nevrax Design Team. Source Code
Powered by Savane 3.9