4

In my model there are an entity generator, some attribute function(out_attrName) and an entity sink. How do I get the sum of all attrName values of every entity? I want to get the value of every entity before it gets to the sink and accumulates this value.

I tried to use a 'Cumulative Sum' block but no luck. This block requires discrete time on the input, so I use Discrete-Time Integrator. Can't say I use it in correct way: for example if values to sum are 34 and 40, the total sum can be some thing like 12344 instead of correct 74.

EDIT:

Example:
consider the following model: 10 entities go to the server and then to the sink.
There are 2 Set attribute blocks:

  • First one for StartTime (current time from Clock, before the server)
  • Second one for EndTime (current time from Clock, after the server)

One Attribute function block to set ServiceTime attribute = EndTime - StartTime.
The model is pretty simple, so ServiceTime attribute is always equal 10. We can see it on the Signal scope.
We've got 10 entities. In each entity there is an attribute ServiceTime == 10. Now I want to get sum of ServiceTime attributes for all entities. 10*10=100. How do I do that?


Details:

  1. Model
  2. Set attribute 1
  3. Server
  4. Set attribute 2 & Script function
  5. Get attribute
Eitan T
  • 32,660
  • 14
  • 72
  • 109
lak-b
  • 2,115
  • 4
  • 18
  • 30
  • 2
    I don't quite understand. Where do the `attrName` come from? Have you tried simply to put all the desired values in a vector and then used the `cumsum` command e.g.? I think I'm missing something in your question... – Steeven Jun 25 '12 at 06:20
  • @lak-b Can you give an example? – Eitan T Jun 25 '12 at 08:46
  • 1
    @Steeven attrName is a value of entity attribute. How can I put value to the vector in model? – lak-b Jun 25 '12 at 19:14
  • Could you explain what you mean by this sentence in your first part of the question: `if values to summ are 34 and 40, the total sum can be some thing like 12344 insted of correct 74`. I don't see what you mean by the values to sum. Is this an example where you only have those two values? Or are there meant to be additional values in between those two? – Steeven Jun 25 '12 at 20:13
  • I'm also not sure what you are seeking to obtain? When you say `In each entity there is an attribute ServiceTime == 10. Now I want to get sum of ServiceTime attributes for all entities`, as far as I understand this sentence, the `ServiceTime` always equals 10. But this means you should just multiply 10 with the number of entities to get the total sum of all of them... But I think I understood that wrong? – Steeven Jun 25 '12 at 20:23
  • 1
    @Steeven It is just an example. ServiceTime is always equal to 10 only in this simple model. In my real model the value of this attribute could be equal to anything, for ex: in first entity attr value = 14, in second one = 9, in third = 12 and so on. So I have to get every entity, take the value of ServiceTime attribute from it and summirize it in some accamulator. But I don't know how to perform such operation. – lak-b Jun 25 '12 at 20:49

2 Answers2

2

After some research here is my own answer that works for me.
Please comment this answer if I wrong in some point.

  1. We need an accumulator (some storage) to store data. So we need to use MatLab work space for that. We can read/write values from it with functions coder.extrinsic('evalin') and coder.extrinsic('assignin').

  2. We have to put all values from all entities in single vector. After this operation we'll have data in one place and can do whatever we like to. This vector will be "hosted" by work space.

  3. In my case it's easy to assign vector elements by index. So there is an ID for every entity (it is the #d value from generator).

  4. Finally, lets write data to vector. Before starting the model, execute this code in Matlab:
    someVar = zeros(1000,1)

Saving data in Attribute function block right before sink:

out_EntityDuration = FinishTime - StartTime;

coder.extrinsic('evalin');
coder.extrinsic('assignin');

x = zeros(1000,1);
x = evalin('base', 'someVar');

x(Id+1) = out_EntityDuration;

assignin('base','someVar',x);

See more about read/write to workspace here http://www.mathworks.com/matlabcentral/newsreader/view_thread/263578

After execution the model someVar store data. Now we can find sum or average value.

Matt
  • 12,848
  • 2
  • 31
  • 53
lak-b
  • 2,115
  • 4
  • 18
  • 30
  • @arttronics it's an answer :) I'm noob in MatLab and may be some of my points are not so smart. But there is complete solution to my question. – lak-b Jul 01 '12 at 21:46
  • Thanks for clarifying that your posted Answer is the solution your using and you've wanting feedback on that. Then please Accept this Answer it so this Question is resolved. That being said, getting feedback for a resolved question is difficult since the Question is ranked as such for SO. In that case, post a new Question (optionally reference this page) with your current process and ask if there is a better method to use and leave that Question open until a better method comes along. I'll upvote your new Question. Also, +1 for this Answer. Cheers! – arttronics Jul 01 '12 at 21:58
  • **Fairness:** You have already lost your 200 Bounty and you will not be able to reclaim that for yourself on your own answer. I recommend awarding that bounty to me after you have accepted your answer. Then at any time I will Sponsor any **current** or **future** SO Question you choose for the same 200 rep. This way, you don't loose out. – arttronics Jul 01 '12 at 22:08
0

It seems like your SO Question is asking "How to get the propagation delay of the sink with Matlab?"

This Matlab Newletter article titled Determining Sample Propagation Delay through a Discrete System discusses the method to use for a Discrete Simulink model in acquiring the total delay value.

According to that newsletter, it's recommended to "use the cross correlation between the input and output feature of Matlab instead of the common analystic estimates for delay since that is difficult to compute into a final value.

arttronics
  • 9,957
  • 2
  • 26
  • 62