Simulink: Get current block path in a way that's supported for code generation
27 views (last 30 days)
Show older comments
I'm trying to create a block that takes a signal as an input and outputs both the signal and it's name (propagated label) as strings:
% Input: Signal
% Output: Signal->String, Name
Converting the signal to a string was straight forward. However, obtaining it's name not so much. I wrote the following Matlab function to get the name of the signal and output it as a string:
function signalName = getSignalName(~)
coder.extrinsic('gcb');
coder.extrinsic('get_param');
coder.extrinsic('strings');
names = strings(1,2);
names = get_param(gcb,'InputSignalNames');
signalName = "";
signalName = char(names(1));
This works, but since I'm using gcb, this function can not be used for code generation. Is there a way to get the current block path as you'd do with gcb but in a way that's compatible for code generation? If not, any ideas on how I can achieve what I described above differently?
1 Comment
Altaïr
on 6 Jan 2025
Edited: Altaïr
on 6 Jan 2025
Assuming that generating C code is the goal, it's important to note that signals and blocks in a Simulink model translate to variables, structures, and functions in C code. Since C doesn't have direct equivalents for blocks and signals, functions like gcb and get_param aren't supported for code generation. While a direct workaround for creating such a block with code generation support might not be straight forward (if there exists one), providing more information about the overall model and the specific need for this block would be helpful to suggest an alternative approach.
Answers (1)
交感神経優位なあかべぇ
on 9 Jan 2026 at 14:37
マスクの初期化関数で入力信号名を取得することで、希望する機能を実装することができると思います。
マスクの初期化関数は、コード生成の対象外であり、Simulinkのコンパイル直前に呼び出されますので、入力信号名をSimulinkモデル内に反映させることができます。

0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!