Unable to get Embedded MATLAB function to work in Simulink

1 view (last 30 days)
When I run this code in the MATLAB command window is operates effectively. When I try to operate it in a simulink file, and the code is in an embedded MATLAB function block, it will not toggle the outputs (outputs do not change when viewed on the scope as it does when you run the code the command window.)
My code is shown below:
% code
function [S1, S2, S3, S4] = fcn( sin, C, V1, V2, V3, V4) % Each S output is related to the corresponding V ( i.e S1-V1, S2-V2 etc)
S1=0; % all set outputs to zero (states of outputs either 1 and 0)
S2=0;
S3=0;
S4=0;
%#codegen
VU=[V1,V2,V3,V4];
S=[S1,S2,S3,S4]; % switched outputs either 0 or 1.
while sin==C
[Vs, A]=sort(VU) % This command for declaring when to 'sort' VU' followed by run the 'for loop'
for x= 1:4 % the for loop for sorted inputs
if S(A(x))==0 % if called x=0
S(A(x))=1; % called x=1
end
end
end
Editor sees no issues with the code and it compiles fine. I am certain it must an issue with the configuration parameters: The main settings are; type 'Variable-step' and solver 'ode45' . I'm using constant values which go into the V1:V4 inputs. The outputs S1:S4 are connected to a scope.
I will be very greatful if there is a way to get this work, and any suggestions are very welcome.
Regards
  3 Comments
Simon Fletcher
Simon Fletcher on 31 Jul 2013
Hi Denis, thanks for your response, yes I have used breakpoints, and it is S4 which is the first switch tested to check it is 0. However, it is already 1 in the if statement, shouldn't it become a 1 on the line after the if statement? (A bit rhetorical, this is the intention). Also there must be a way to define the outputs to go into the output.
Thanks
Denis Gurchenkov
Denis Gurchenkov on 1 Aug 2013
Perhaps I'm misreading the example, but it looks like the "while" loop either runs for 0 iterations (if sin and C are not equal), or runs forever (if sin == C). In the first case, S1..S4 will not change (all zeros), in the second case the program will never terminate (forever loop). That's not what you expect, right?
If you can attach your Simulink model, your MATLAB file and tell me how you run it (what are the inputs), perhaps it'll be easier for me to understand the problem then.
-- Denis

Sign in to comment.

Answers (0)

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!