C Caller error when passing 2D array inside struct via Simulink bus

25 visualizzazioni (ultimi 30 giorni)
I have a variable in C header defined as
typedef struct {
double a[1][4];
} temp
and a C function:
void Fun(Temp* temp) {
}
In Simulink, I try to send 1x4 data packed into a bus and pass it to the C Caller block, which calls Fun() as shown in the below images.
However, when building, I get the following errors:
m_6ytl4aiSKJUAISadDdLIqD.c
m_6ytl4aiSKJUAISadDdLIqD.c(52): error C2440: '=': cannot convert from 'float64 [4]' to 'real_T'
m_6ytl4aiSKJUAISadDdLIqD.c(57): error C2440: '=': cannot convert from 'float64 *' to 'real_T'
Interestingly, if I do not use the struct and bus, and instead directly pass a pure 1x4 array like this:
void Fun(double a[][4]) {
}
then it works fine.
So, the error occurs only when the 2D array is packaged inside a struct and transmitted via a Simulink bus.
Could you help me understand why this happens, and how to properly map the bus signal to the struct array in C Caller?
Thank you!

Risposta accettata

Umar
Umar il 16 Set 2025 alle 15:16

Hi @ 주형,

I understand the compilation error you're encountering with the C Caller block when passing 2D arrays within structs via Simulink buses. This is a well-documented limitation in the Simulink-to-C interface mapping.

The error occurs due to fundamental differences in how Simulink represents multidimensional arrays versus how C handles them within structures:

1. Simulink Bus Representation: Simulink buses handle 2D arrays differently when they're embedded within structures compared to standalone arrays 2. C Caller Interface Mismatch: The generated code attempts to assign float64 [4] (Simulink's representation) to real_T (single scalar), indicating incorrect type mapping

So, I have listed technical solutions to this root cause below.

Method 1: Use Simulink.importExternalCTypes (Recommended) Use the Simulink.importExternalCTypes function to automatically generate proper Simulink representations of your C struct types:

% In your initialization script
Simulink.importExternalCTypes('your_header.h', ...
  'DataDictionary', 'your_dict.sldd', ...
  'OutputDir', 'output_directory');

This will create the correct bus definition that matches your C struct exactly.

Method 2: Manual Bus Creation with Proper Dimensions If automatic import isn't suitable, manually create the bus with explicit array dimensions:

% Create bus element for the 2D array
busElem = Simulink.BusElement;
busElem.Name = 'a';
busElem.DataType = 'double';
busElem.Dimensions = [1 4];  % Explicit 2D dimensions
% Create the bus object
tempBus = Simulink.Bus;
tempBus.Elements = busElem;
% Save to data dictionary or workspace

Method 3: Alternative C Function Signature Consider modifying your C function to accept the array parameter directly:

// Instead of:
typedef struct {
  double a[1][4];
} temp;
// Use:
void Fun(double a[][4]) {
  // Your implementation
}

This approach works because direct array passing doesn't involve the struct-to-bus mapping complexity.

So, my recommendations at this point would be

1. Use importExternalCTypes: This function is specifically designed for integrating existing C code structures with Simulink buses 2. Validate Bus Definitions: Always verify that generated bus objects match your C struct layout exactly 3. Consider Array Flattening: For complex 2D arrays, consider flattening to 1D and reshaping within your C function

Verification Steps

1. Check generated bus element dimensions match [1 4] 2. Verify data type mapping (double in C → double in Simulink) 3. Confirm C Caller block parameter configuration matches function signature

The fundamental issue is that Simulink's bus-to-struct mapping has limitations with multidimensional arrays embedded in structures. The automatic import function typically resolves these mapping inconsistencies. Best regards,

*Reference: * MathWorks Documentation - Simulink.importExternalCTypes, C Caller Integration

Hope this should help resolve your issues.

  1 Commento
주형
주형 il 25 Set 2025 alle 4:42
Thank you very much Umar!
I tried all approaches.
I think that Simulink can't generate [1][4] array. It seems that interpretes [1][4] as same with 1D array: [4].
However, your solution is working when [n][4] array except n=1.
I think that I need to avoid [1][n] array size in C.
Thanks!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Simulink Functions in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by