Matlab function in simulink

4 次查看(过去 30 天)
Vimala Starbino
Vimala Starbino 2020-9-18
回答: Pramil 2024-11-26,10:11
Errors occurred during parsing of MATLAB function 'MATLAB Function'(#35)
Error in port widths or dimensions. Output port 1 of 'rectinput/MATLAB Function/d' is a one dimensional vector with 1 elements.
my coding is
function y = fcn(a,b,c,d)
%#codegen
v = rectangle('Position',[a b c d]);
axis ([0 10 0 10]);
y = v;

回答(1 个)

Pramil
Pramil 2024-11-26,10:11
Hey Vimala,
The error you're encountering is related to the output dimensions of the "MATLAB function" block.
In your code, you're trying to assign the output "y" to the result of the "rectangle" function, which doesn't return a numeric value but rather a handle to the rectangle object. This is likely causing the mismatch in expected output dimensions.
If your goal is to visualize a rectangle and not return any specific numeric output, you might want to adjust your function so that it doesn't attempt to return the handle.Here's a modified version of your function:
function fcn(a, b, c, d)
%#codegen
rectangle('Position', [a, b, c, d]);
axis([0 10 0 10]);
end
If you need to return some specific information about the rectangle, such as its position or size, you can adjust the function accordingly. Here's an example of returning the position vector:
function pos = fcn(a, b, c, d)
%#codegen
rectangle('Position', [a, b, c, d]);
axis([0 10 0 10]);
pos = [a, b, c, d]; % Return the position vector
end
Hope it helps.

Community Treasure Hunt

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

Start Hunting!

Translated by