Why isn't there a bsxfun block in Simulink?

Recently, I have found myself adding 1-line MATLAB Function Blocks to some models, of the form (though not necessarily always with @times):
function y=fcn(u, v)
y=bsxfun(@times, u, v);
I have made some attempts at implementing a general bsxfun block that would let me choose the function from a list, but I can't figure out how to pass the function handle from a mask into the MATLAB Function Block.
So why does this not exist (I have searched)?
Can anyone show me a way to construct the block I have described, without resorting to S-functions (we try to avoid them)?
Thanks!

답변 (1개)

Ryan G
Ryan G 2013년 6월 25일
편집: Ryan G 2013년 6월 25일

1 개 추천

Why doesn't the product block work? It can perform element by element operations. If you are using it for the singleton functionality, you could just copy the smaller array yourself to match the larger array using the selector block.
EDIT: This can also work for other functions supported by bsxfun.

댓글 수: 4

Nathaniel
Nathaniel 2013년 6월 25일
Yes, I'm using bsxfun for the singleton expansion functionality, sorry for not explaining that.
I'm using bsxfun in hopes of improving efficiency, and copying things seems at odds with that goal.
Another you option you have is using a gain block. Say I have a vector:
[1 2 3 4]
coming into the gain block. I can use the matrix multiplication option (K*u) and set the gain block to:
[1 1 1 1]'
This would result in the initial data being copied to a 4x4. You can modify this example into what you're working with. One thing to remember about Simulink vs. MATLAB is that Simulink requires more well defined dimensions where MATLAB is open and flexible. This is why I personally would not expect a function like bsxfun in Simulink, even if it's technically possible. Chances are you may be later interested in code gen for deployment or hardware in the loop and your matrices should be well defined for this.
If your data sizes and signal sizes are well defined, this workaround (or the selector block) should not be to much of a problem.
Nathaniel
Nathaniel 2013년 6월 25일
Thanks, but I'm really looking for a way to do what I asked (i.e., using bsxfun), or a really good explanation for why it can't be done, or is a bad idea.
Ryan G
Ryan G 2013년 6월 25일
There is not anything that I could find capable of the automatic singleton expansion bsxfun performs. There are those workarounds mentioned which may be ok if you want to avoid s-functions. I would suggest putting in an enhancement request and the tech support team could give you a better answer if it's not technically possible (it may be possible just not created).
One last option you could try, but this won't stop you from having to use s-functions. Create a subsystem with the MATLAB function inside of it, then use the right-click generate s-function option. This will create a block for you that runs a mex-file. You can add this to a utilities library along with the rest of your project. This option may be considered a bit cleaner than MATLAB function blocks...

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품

질문:

2013년 6월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by