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)
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
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))?
Benjamin Guinot
Benjamin Guinot on 9 Mar 2011
Hi Andrew,
Looks like I haven't been clear enough... It's actually quite hard to explain...
Basically this 'vector type' property (you're right it is actually more a vector than a matrix) is of size n (nx1 double). I have a separate script which uses an instance of the class described above. This script is a for-loop type where Row_ID is used to designate the current row of the 'vector property'. At each time step (referenced by Row_ID) the value of the 'vector property' for this particular row is calculated. What I would like is a listener capable, at each time step, to detect if the value just calculated (at Row_ID) has exceeded a given value (+ I need to know which Row it is). The problem is that in the set.property method I can access the 'vector property' as a whole but since Row_ID is defined in the script and not in the class I cannot use it within the set.property method
The only solution I see would be to define a property within the class (let's call it Row_ID2) and at each time step to assign the Row_ID value to Row_ID2 (My_Obj.Row_ID2 = Row_ID)
Any better suggestion? (Thanks anyway for your time)

Sign in to comment.

Accepted Answer

Andrew Newell
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
Benjamin Guinot
Benjamin Guinot on 11 Mar 2011
Hi Andrew,
Thanks for your help. I have to admit that it might look tricky. But thinking again you are right and I can probably include the test "if x(i) > maxValue" within the for loop as you did. But by doing so I was wondering if the code would run slower if at each time step it has to test the variable x(i) (if x(i) > maxValue) (the vector might contain more than 500,000 rows). Would the use of a listener take the same time? (my knowledge are quite limited on this point and both solutions are probably totally equivalent from a running time point of view)
Andrew Newell
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.

Sign in to comment.

More Answers (1)

Andrew Newell
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))])

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!