Logo Simulink Use C Function Block

Simulink Use C Function Block

Tổ chứcPhổ biến
matlab
simulink-use-c-function-block

Dùng khi tích hợp mã cũ, mã tùy chỉnh hoặc mã C/C++ vào Simulink thông qua các khối C Function. Cấu hình các khối Simulink C Function bằng lập trình với API SymbolSpec. KÍCH HOẠT khi người dùng: - Hỏi cách gọi hoặc bọc mã hay thư viện C/C++ trong Simulink - Muốn tạo, cấu hình hoặc sử dụng khối C Function - Đề cập đến việc tích hợp mã tùy chỉnh, mã cũ hoặc mã C/C++ bên ngoài vào mô hình - Hỏi cách tích hợp lớp C++ vào mô hình Simulink hoặc băng thử - Đề cập đến đầu vào/đầu ra/tham số của khối thuật toán C++

Add to TypingMind

Tổng quan

Nhà phát hànhmatlab
Kho mãsimulink-agentic-toolkit
Tên kỹ năngsimulink-use-c-function-block
Sao
1.2K
Bản fork
112
Tệp đi kèm
3
Liên kết
  • Hướng dẫn bằng Markdown

    Tệp SKILL.md được mô hình tải khi cần, nên chỉ tốn token khi có yêu cầu thực sự phù hợp.

  • Dùng được với mọi LLM

    Kỹ năng AI dùng hướng dẫn viết bằng Markdown thay vì mã dành riêng cho một nhà cung cấp, nên dùng được với GPT, Claude, Gemini, Grok hoặc mô hình cục bộ.

  • 3 tệp đi kèm

    Các tập lệnh, mẫu và tài liệu tham khảo mà mô hình có thể đọc khi làm việc. Các tệp chỉ cho phép đọc và không bao giờ được thực thi.

  • Mã nguồn mở

    Được matlab công bố trên GitHub. Hãy đọc các tệp nguồn trước khi cài đặt.

Cài đặt

Cài đặt kỹ năng AI Simulink Use C Function Block trong TypingMind để dùng với bất kỳ LLM nào, hoặc thêm vào một tác nhân AI khác có thể đọc SKILL.md.

1

Cài đặt trong TypingMind

TypingMind cài đặt kỹ năng trực tiếp từ thư mục GitHub — đọc SKILL.md, đóng gói các tệp tài nguyên và lưu kết quả trên máy.

  1. Mở ứng dụng và vào Plugin → Kỹ năng.
  2. Chọn "Cài đặt từ GitHub".
  3. Dán URL của thư mục kỹ năng được hiển thị bên dưới rồi xác nhận.
    https://github.com/matlab/simulink-agentic-toolkit/tree/main/skills-catalog/simulink-modeling/simulink-use-c-function-block
  4. Bật kỹ năng trong từng cuộc trò chuyện mà bạn muốn sử dụng kỹ năng đó.
Plugin → Kỹ năng → Thêm kỹ năng → Từ URL GitHub, sau đó dán URL thư mục và nhấn Tiếp tục.
2

Cài đặt trong tác nhân AI khác

Bất kỳ tác nhân AI nào đọc được định dạng Agent Skills đều có thể dùng kỹ năng này — hãy sao chép thư mục vào thư mục kỹ năng của tác nhân AI đó.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/matlab/simulink-agentic-toolkit.git /tmp/simulink-agentic-toolkit
mkdir -p .claude/skills
cp -r /tmp/simulink-agentic-toolkit/skills-catalog/simulink-modeling/simulink-use-c-function-block .claude/skills/simulink-use-c-function-block
Khởi động lại Claude Code sau khi sao chép để ứng dụng nhận kỹ năng mới.

Sử dụng trong TypingMind

Bật Simulink Use C Function Block trong bất kỳ cuộc trò chuyện nào trên TypingMind, mô hình sẽ tự xử lý phần còn lại. Tên và mô tả của kỹ năng nằm trong prompt hệ thống. Ngay khi có yêu cầu phù hợp, mô hình tự tải toàn bộ hướng dẫn — bạn không cần gọi thủ công và kỹ năng không tốn token cho đến khi thực sự được dùng.

Mô hình tự nạp Simulink Use C Function Block ngay khi có yêu cầu phù hợp.

Dùng được với mọi mô hình AI

Kỹ năng AI là các hướng dẫn Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên bạn có thể dùng Simulink Use C Function Block với cả những mô hình khác ngoài mô hình ban đầu. Chỉ cần cài đặt một lần trong TypingMind rồi dùng với GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama hoặc mô hình cục bộ do bạn tự chạy — tất cả đều dùng khóa API của riêng bạn.

  • Chỉ tải khi cần

    Prompt hệ thống chỉ chứa tên và mô tả. Hướng dẫn được tải khi có yêu cầu phù hợp đầu tiên, nên khi kỹ năng chưa được dùng, bạn không tốn token cho phần hướng dẫn.

  • Đổi mô hình ngay trong cuộc trò chuyện

    Vì kỹ năng là hướng dẫn chứ không phải mã, việc đổi mô hình không làm kỹ năng ngừng hoạt động — mô hình tiếp theo vẫn đọc cùng tệp SKILL.md.

Hướng dẫn của kỹ năng

Đây là nội dung SKILL.md mà mô hình tải. Hãy đọc trước khi cài đặt — kỹ năng là những hướng dẫn mà mô hình của bạn sẽ làm theo.

C Function Block Configuration

When to Use

  • Calling or wrapping C/C++ code or libraries in Simulink
  • Creating, configuring, or using a C Function block
  • Integrating custom code, legacy code, or external C/C++ into a model
  • Integrating a C++ class into a Simulink model or test bench
  • Configuring inputs, outputs, or parameters for a C++ algorithm in a block

When NOT to Use

  • Building general Simulink models without C/C++ integration
  • MATLAB Function blocks (Embedded MATLAB) — those use MATLAB syntax, not C/C++
  • Code generation workflows (Embedded Coder, Simulink Coder) — this skill is for simulation-time configuration only

Rules

  • Derive each symbol's Simulink type from the corresponding C/C++ function parameter/return type (e.g. float → single, bool → boolean, char*/char[] → string (R2024b+)). However, when replacing an existing block (e.g. an S-Function), use the types already on that block first — for example, what looks like int32 from C may actually be fixdt(1,32,3) or another fixed-point type in the existing model.
  • MANDATORY for C++ classes: When the user's code defines a C++ class, you MUST first attempt to declare a Persistent symbol of Class: ClassName and call class methods directly in the code sections (e.g. obj.Method(args)). If the class has an init method (not a constructor), call it in StartCode. Only fall back to wrapper/handler functions or opaque pointer patterns if the class approach fails to compile.
  • Prefer relative paths for custom code files (sources, headers, libraries, search directories). Use paths relative to the model file location (e.g. 'myFunctions.h', './src/myLib.cpp') rather than absolute paths. This ensures portability across machines and users.

Workflow Overview

  1. Add the C Function block — use model_edit with type "C Function" and set CustomCodeSettingLocation (default: "BlockSettings")
  2. Configure custom code settings — point to headers/sources either on the block or model config (see references/custom-code-config.md)
  3. Set code sections — configure OutputCode, StartCode, etc. on the block
  4. Configure symbols via SymbolSpec — use model_query_params to get the SymbolSpec, then evaluate_matlab_code for object operations

SymbolSpec API (Port & Symbol Specification)

The SymbolSpec object defines the block's inputs, outputs, persistent state, constants, and parameters.

Getting the object

Query with model_query_params, then operate via evaluate_matlab_code:

matlab
obj = get_param('myModel/MyCFunction', 'SymbolSpec');

Adding, getting, deleting symbols

matlab
symObj = obj.addSymbol('varName');       % adds with default: Input, double, size '1'
symObj = obj.getSymbol('varName');       % get one by name
allSyms = obj.Symbols;                  % get all symbols
obj.deleteSymbol('varName');

Note: addSymbol always creates with defaults (double, Input, size '1'). When re-adding a deleted symbol, explicitly set all non-default properties (Type, Scope, Size) — prior properties are not retained.

Symbol Properties

PropertyValuesDefaultNotes
Nameidentifier string(from addSymbol)Variable name in code; for class types includes constructor args
Scope'Input', 'Output', 'InputOutput', 'Persistent', 'Constant', 'Parameter''Input'See Scope Details below
Type'double', 'single', 'int8'...'uint64', 'Boolean', 'string', 'Bus: BusName', 'Enum: EnumName', 'Class: ClassName', 'AliasTypeName', or fixedpoint'double'
Sizedimension string'1'e.g. '1', '3', '[2,3]', 'size(u1)'
Labelport label / valuesame as NameFor Constant: the literal value
PortNumberuint32auto-assignedPort ordering

Scope Details

ScopeDescriptionPort?
InputBlock input portYes (inport)
OutputBlock output portYes (outport)
InputOutputRead-write pass-through (always first in port numbering)Yes (both)
PersistentState that persists across time steps (also used for class instances)No
ConstantCompile-time constant (scalar only)No
ParameterTunable parameter from workspace/maskNo

Setting Parameter values

After setting Scope to 'Parameter', set the value by configuring the block with the parameter name as the key.

C++ Class Types

Class instances are declared as Persistent scope symbols with Type = 'Class: ClassName'.

Constructor arguments are specified in the Name property:

matlab
obj = get_param('myModel/CFunction', 'SymbolSpec');

% Default constructor (no args)
sym = obj.addSymbol('myObj');
sym.Scope = 'Persistent';
sym.Type = 'Class: MyClass';

% Constructor with arguments (args must be Parameter or Constant scope symbols)
sym = obj.addSymbol('myObj(p)');
sym.Scope = 'Persistent';
sym.Type = 'Class: MyClass';

Rules for class types:

  • Class must be defined in an external header (included via custom code settings) — NOT inside code sections
  • Cannot create class instances as local variables in code sections — applies when CustomCodeSettingLocation = 'ModelConfigurationParameters' (always parsed), or 'BlockSettings' with ParseBlockCode = 'on'
  • Constructor arguments must be Parameter or Constant scoped symbols only
  • Function calls in constructor arguments are not allowed
  • new operator is not allowed when block code is parsed
  • Private/protected members cannot be accessed from code sections
  • Class type does not support save/restore (SimSnapshot)

Access class methods in code sections: y = myObj.compute(u1);

Size Expressions

  • Scalar: '1'
  • Vector: '3' or '[3,1]'
  • Matrix: '[2,3]'
  • Dynamic from input: 'size(u1)', 'size(u1,1)', 'size(u1) + 1'

Rules:

  • size() IS allowed in output dimensions (compute output size from inputs)
  • size() is NOT allowed in input dimensions
  • Output size CANNOT be '-1' (inherited) — must be explicit or use size() expression
  • Input size CAN be '-1' (inherited)
  • Size must evaluate to integer values
  • Nested size(size(...)) is NOT supported

Restrictions

  • #include directives NOT allowed inside code sections (Start/Output/Terminate) — use custom code settings
  • Start/Terminate code CANNOT access Input or Output scoped symbols
  • Constant scope must be scalar
  • Library functions (e.g. isalnum) not supported inside code sections when ParseBlockCode = 'on' or CustomCodeSettingLocation = 'ModelConfigurationParameters' — use external calls via header. No restriction under default R2026a+ settings (BlockSettings + ParseBlockCode = 'off')
  • When using a Simulink.NumericType with DataScope = 'Exported' in symbols, or any Simulink.NumericType in code sections, its IsAlias property must be true
  • When CustomCodeSettingLocation = 'UpdateBuildInfo', GenerateCodeAsIs is forced 'on'
  • Class definitions NOT allowed inside code sections — define in external header
  • Variable-size signals are not supported

Full Example: Block-Level Custom Code with C++ Function

matlab
% 1. Add C Function block with block-level custom code
%    Use model_edit: add_block type "C Function", set CustomCodeSettingLocation,
%    SimCustomHeaderFile, SimCustomSourceFile, and OutputCode on the block.

% 2. Configure symbols
obj = get_param('mMyTest/C Function', 'SymbolSpec');
obj.addSymbol('u1');
obj.addSymbol('u2');
u2obj = obj.getSymbol('u2');
u2obj.Type = 'int32';
u2obj.Size = '3';
obj.addSymbol('y');
yobj = obj.getSymbol('y');
yobj.Scope = 'Output';
yobj.Size = 'size(u2)';
obj.addSymbol('state');
stateobj = obj.getSymbol('state');
stateobj.Scope = 'Persistent';
save_system('mMyTest');
close_system('mMyTest', 0);

See references/examples.md for additional patterns (model-level custom code, C++ class with constructor args). See references/custom-code-config.md for detailed parameter tables and custom code location settings.


Copyright 2026 The MathWorks, Inc.


Tệp đi kèm

Khi kỹ năng đã được tải, mô hình có thể đọc các tệp này nếu cần. Các tệp này chỉ dùng để đọc và không bao giờ được thực thi.

Câu hỏi thường gặp

Kỹ năng AI Simulink Use C Function Block làm được gì?

Dùng khi tích hợp mã cũ, mã tùy chỉnh hoặc mã C/C++ vào Simulink thông qua các khối C Function. Cấu hình các khối Simulink C Function bằng lập trình với API SymbolSpec. KÍCH HOẠT khi người dùng: - Hỏi cách gọi hoặc bọc mã hay thư viện C/C++ trong Simulink - Muốn tạo, cấu hình hoặc sử dụng khối C Function - Đề cập đến việc tích hợp mã tùy chỉnh, mã cũ hoặc mã C/C++ bên ngoài vào mô hình - Hỏi cách tích hợp lớp C++ vào mô hình Simulink hoặc băng thử - Đề cập đến đầu vào/đầu ra/tham số của khối thuật toán C++

Vì sao nên dùng Simulink Use C Function Block trên TypingMind?

Vì bạn chỉ cần cài một lần rồi dùng với bất kỳ mô hình nào. Simulink Use C Function Block là nội dung Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên cùng một kỹ năng có thể chạy trên GPT-5, Claude, Gemini, Grok hoặc mô hình trên thiết bị — và bạn có thể đổi mô hình ngay trong cuộc trò chuyện mà kỹ năng vẫn hoạt động. TypingMind dùng khóa API riêng của bạn, nên bạn thanh toán trực tiếp cho nhà cung cấp thay vì trả phí thuê bao theo suất người dùng, đồng thời kỹ năng và các cuộc trò chuyện vẫn nằm trong bộ lưu trữ của bạn.

Làm thế nào để cài Simulink Use C Function Block trong TypingMind?

Mở Plugin → Kỹ năng → Cài đặt từ GitHub trong TypingMind rồi dán https://github.com/matlab/simulink-agentic-toolkit/tree/main/skills-catalog/simulink-modeling/simulink-use-c-function-block. TypingMind đọc tệp SKILL.md, đóng gói các tệp đi kèm rồi cài đặt cả gói dưới dạng một kỹ năng mà bạn có thể bật cho từng cuộc trò chuyện.

Những mô hình AI nào có thể dùng Simulink Use C Function Block?

Bất kỳ mô hình nào bạn kết nối trong TypingMind. Kỹ năng AI là các hướng dẫn bằng Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên GPT, Claude, Gemini, Grok và các mô hình chạy trên thiết bị đều có thể tải kỹ năng này khi có yêu cầu phù hợp.

Có thể dùng Simulink Use C Function Block với bao nhiêu mô hình AI?

Bao nhiêu cũng được. Miễn là mô hình hỗ trợ kỹ năng, bạn có thể dùng Simulink Use C Function Block với mô hình đó — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama và nhiều mô hình khác — tất cả đều trên TypingMind với khóa API riêng của bạn.

Kỹ năng AI Simulink Use C Function Block có miễn phí không?

Kỹ năng này được matlab phát hành trên GitHub. Hãy xem điều khoản giấy phép trong kho mã. Bạn chỉ trả tiền cho nhà cung cấp AI của mình theo số token đã dùng.

Kỹ năng AI là gì?

Kỹ năng AI là một gói hướng dẫn có thể tái sử dụng, chỉ cho mô hình AI cách thực hiện một tác vụ cụ thể. Kỹ năng tuân theo định dạng mở Agent Skills: một tệp SKILL.md chứa tên và mô tả, cùng các tập lệnh, mẫu hoặc tệp tham khảo mà mô hình có thể cần. Mô hình chỉ đọc hướng dẫn khi yêu cầu của bạn phù hợp với kỹ năng, nên kỹ năng đã cài đặt không phát sinh chi phí cho đến khi được sử dụng.

Kỹ năng AI khác gì với plugin hoặc máy chủ MCP?

Plugin hoặc máy chủ MCP cung cấp các công cụ mới mà mô hình có thể gọi. Khi mô hình gọi một công cụ, mã của công cụ đó chạy ở đâu đó và trả về kết quả. Còn kỹ năng AI cung cấp kiến thức và quy trình: cách tiếp cận tác vụ, các bước cần thực hiện và những tiêu chí mà kết quả cần đáp ứng. Kỹ năng chỉ là nội dung Markdown, nên không cần máy chủ, khóa API hay môi trường thực thi và hoạt động với mọi mô hình.

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇