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)
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?

Accepted Answer

Guillaume
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]}}))
However, what I'd recommend is that you add a dependent property to your classB:
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]
  1 Comment
Grant Junno
Grant Junno on 29 Nov 2018
Thanks Guillaume. I can't use logical indexing in my case without performing an operation because what I'm referring to here as boolArray is actually a numerical array and certain conditions determine whether or not the associated infArray element needs to be called up. I tried to simplify things to explain my question which is more general than both the original post's situation and the actual program I'm currently working with. I've wondered if such a shortcut existed before, and it appears subsref is the way to approach that. It does also happen to be, as you stated, pretty ugly so that will play into determing if/when I make use of it.
In the particular case I'm working with now, I think I can achieve the behavior (and ease of use) I'm looking for with your dependent accessor approach though, so thank you for that idea as well!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!