MATLAB Answers

Return values of function vs. assignin

2 ビュー (過去 30 日間)
Luca Tarasi
Luca Tarasi 2019 年 8 月 17 日
回答済み: Nikhil Sonavane 2019 年 8 月 20 日
I'm writing a MATLAB function that sets some variables for a Simulink simulation of a planar robot.
A bunch of values are to be set in the workspace, around 20. However, about half of them should be of no interest for the end user, because they are internal variables of the simulation (variables for Simulink switches, for handling errors in the simulation, etc.). Also, they have to be saved in the workspace with the name that's used in the Simulink model.
Because of this, I was thinking about storing these values not by return values of the function, but by using the assignin function. This way, they could be stored with the right names in the workspace and the end user could ignore them.
Do you think it would be appropriate or just confusing and smell of bad design?
Thank you.

  4 件のコメント

表示 1 件の古いコメント
Luca Tarasi
Luca Tarasi 2019 年 8 月 17 日
(To Bruno Luong's comment) Though so. Thank you!
Stephen Cobeldick
Stephen Cobeldick 2019 年 8 月 17 日
Bad design.
In similar situations I have put all those extra bit-and-pieces into one structure, and passed that as an (undocumented) output argument (right after all the documented ones).
John D'Errico
John D'Errico 2019 年 8 月 17 日
I do agree with the comments by others. If you have many parameters to return, just stuff them into a struct. The idea of using assignin for the purpose is a terrible design choice, IMHO.



Nikhil Sonavane
Nikhil Sonavane 2019 年 8 月 20 日
I understand that you wish to set some parameters in the workspace before you execute your model but don’t wish the end user to do it every time. I suggest you use Callbacks instead of a function. You may refer to the following documentation for more information about callbacks-

  0 件のコメント


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by