I have a vector [-1 1 -4 9 7] stored in a constants block. I also have signal which contains a scalar which is determined from other parts of my Simulink code. Let's call that scalar, 'n'. How do I extract the n'th element of my vector? Example:
Asked
Active
Viewed 1,882 times
1 Answers
2
The easy solution I can think of is making a custom block and just adding a Multiport Switch to it.

Ander Biguri
- 35,140
- 11
- 74
- 120
-
Thank you very much! Is it not possible using default Simulink blocks? I would strongly prefer to do it this way. Failing that, is it possible to elaborate a bit more on how to create the custom block and how it should interact with the Multiport switch? Thanks again for your help :) – Matthew James Apr 01 '20 at 16:22
-
@MatthewJames ah, that is for tydiness only. Multiport Switch is a default SImulink. You can just make "functions" by putting them inside a subsystem, that is what I meant. – Ander Biguri Apr 01 '20 at 17:39
-
Would you mind kindly showing me a block diagram of how this will work - or even better an extremely simple version of this working? I can't seem to figure it out :( thanks so much – Matthew James Apr 01 '20 at 18:18
-
Its just a thing to have simulink models tidy. See the tutorial: https://uk.mathworks.com/help/simulink/subsystems.html – Ander Biguri Apr 01 '20 at 19:08
-
Thank you again for your input. I've managed to solve the problem with a multi switch block like you recommended. However, I have an additional complication. The number 'n' is a "variable-size mode", and the multi switch will always output an error. Can you think of a way to solve this? Thank you :) – Matthew James Apr 06 '20 at 09:26
-
And how would you define a switch with variable size input? if `n` is `[3 1 51 2 5 1 1 1 ]`, what is the output? You are askign a different question so I suggest you do so in SO, but you will need to define the behaviour you want – Ander Biguri Apr 06 '20 at 09:28
-
Oh! I finally get it! I thought variable size meant that that actual value of n can change. Instead it means that the size of the vector/matrix/etc changes. Easy fix! I get it :) Thank you so much. – Matthew James Apr 06 '20 at 09:56
-
I have one last final question about something different but related. I now need to extract an element of a matrix (say [1 2 3; 4 5 6; 7 8 9]) and I have the row and column number (say 2,3). How can I use multi-port switch to do this? Thanks again for all your continuous help. – Matthew James Apr 06 '20 at 12:39
-
I have no idea if it works with a multiport swicth, as what it does is connect one signal to the output. maybe you can create linear indices instead of subscripts. – Ander Biguri Apr 06 '20 at 12:53