Is there a way to access a specific element of an output array without assigning the output to a variable first?
5 views (last 30 days)
Show older comments
I'm not certain exactly how to word this question, so I'll go with an example.
I have two classes, we'll call them ClassA and ClassB. ClassA has an array of ClassB objects stored as one of its properties, and ClassB inherits handle so I am using one instance of ClassA to query and control many ClassB objects. We will call the property which is an array of ClassB objects ClassA.CBO. ClassB has a property which itself is an array with some logical values in it. We'll call this property ClassB.boolArray. ClassB has another array of information, ClassB.infArray, which is used to populate ClassB.boolArray in such a way that
ClassB.boolArray(n) == 1
tells me that ClassB.infArray(n) holds a value I need to pass to another function. If any of my ClassB objects have a true value in any position of ClassB.boolArray, I need to use that to call a function using the asoociated ClassB.infArray elements.
I can use
inds = find([ClassA.CBO.boolArray] == 1)
to find indices where boolArray is true, and I can assign a variable with all of the ClassB.infArrays put into a vector,
var = [ClassA.CBO.infArray]
, then access
var(inds)
to return the values I want from infArray (that is, the values in any position of any infArray of any ClassB object associated with that same ClassB object's boolArray).
So, my question: Is there any way to approach this in a more direct manner, skipping the "var" assignment? Something akin to (code I know does not work)
[ClassA.CBO.infArray](inds)
, combining the "make a vector out of this group of arrays" and "access an index of that resulting vector" steps? If such a method exists (and there aren't obvious help pages available to answer this question once I know what to call it), are there reasons not to use such an approach?
0 Comments
Accepted Answer
Guillaume
on 29 Nov 2018
Edited: Guillaume
on 29 Nov 2018
Yes, [ClassA.CBO.infArray](inds) attemps to index the return value of a function, which matlab does not allow. You could do the indexing explicitly by calling subsref:
subsref([classA.CBO.InfArray], struct('type', '()', 'subs', {{inds}})); %subs must be a cell array for () indexing. Have to use {{ inside the struct call to create a cell array otherwise it is interpreted as a request to create a struct array
which is incredibly ugly, I wouldn't recommend it. Note that you don't need to call find, you can pass directly the logical array [ClassA.CBO.boolArray]:
subsref([classA.CBO.InfArray], struct('type', '()', 'subs', {{[ClassA.CBO.boolArray]}}))
classdef classB
properties %your normal classB properties
InfArray;
boolArray;
end
properties (Dependent)
filteredInf
end
methods
%your standard methods
%function this = classB(varargin) %constructor
%etc.
%accessor for dependent property:
function filtered = get.filteredInf(this)
filtered = this.InfArray(this.boolArray); %no need for find and certainly no need for == 1
end
end
end
Then you can go back to normal matlab syntax:
[classA.CBO.filteredInf]
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!