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.