Problem when using set.property method on a property of matrix type ? How to access the value of only one element of the matrix within the set.property method?
2 views (last 30 days)
Show older comments
Hi everyone,
This is a general question on using the set.property method when the property is a matrix (contains several elements and thus several values)
Problem: I have a Class "Test" with a property "Matrix". This property is a nx1 matrix. During code execution I create an instance of this class and the code modifies each row of this property "Matrix" one after the other. I would be interested in adding a listener in order to detect when one of the Matrix element exceeds a given value. However, when setting-up the set.Matrix method I cannot access a particular row as my "Row_ID" indicator is not defined within the class. I can only access the property "Matrix" entirely but looses information on the current Row_ID.
Do I have to include the "Row_ID" as one of the class property to be able to access ii within set.Matrix ? Even if using other properties within a set property method is not recommended (Matlab help) ?
Any suggestion on a way to integrate this listener would be much appreciated...
Ben
2 Comments
Andrew Newell
on 4 Mar 2011
What you seem to be saying is that, as you modify the matrix (really a vector) element by element, you want the listener to know when *any* element exceeds a certain value. Is that right? Do you need to know which element exceeds the value, or will it do to look at MAX(ABS(MATRIX))?
Accepted Answer
Andrew Newell
on 9 Mar 2011
@Benjamin, this seems like a very tricky way to implement something that could be done with a simple for loop. I hope there some deeper purpose to this. You can't feed Row_ID to set.Matrix because the get/set interface insists on property/value pairs. I can't think of a better way to do this than to index the row within the class. Here is an example without the listener:
classdef testTimeSequence < hgsetget
properties
x % your vector
end
properties (Access=private)
rowNumber = 0;
end
methods
function obj = testTimeSequence(x0)
obj.x = x0;
end
function set.x(obj,newValue)
n = get(obj,'rowNumber');
if n == 0
obj.x = newValue;
else
obj.x(n) = newValue;
end
set(obj,'rowNumber',n+1)
end
end
end
You would set this up with a command like
h = testTimeSequence(zeros(100,1));
and each step of the loop would involve a command like
set(h,'x',newValue)
2 Comments
Andrew Newell
on 11 Mar 2011
The loop is probably much faster. For the if test, all it has to do is look up an address. There is much more overhead in calling the class.
More Answers (1)
Andrew Newell
on 9 Mar 2011
And here is a simple for loop that does everything you want:
x = zeros(100,1);
maxValue = 10;
for i=1:length(x)
x(i) = input('Next number: ');
if x(i) > maxValue
break
end
end
disp(['Maximum exceeded at index number ',num2str(i)])
disp(['Value at this point = ',num2str(x(i))])
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!