synchronization of matlab function blocks

2 次查看(过去 30 天)
Dear all, I have a simulink model who contains 9 subsystems connected to each other via GoTo-From blocks, each subsystem has a matlab function block (should be the same block, I just copy past from the first subsystem to the other subsystems), my question is regarding the matlab function block, I need to change some variables inside the matlab function in the subsystem number one these variables should be changed instantly in the other blocks as well, I was looking for a way to define the matlab function block to be global or on other words how can I tell the simulink that this matlab function block is the same block in all subsystems so any change in any block either adding a code or updating variables during the simulation time will be instantly applied to the other blocks.
Also I have a question regarding the output of the matlab function block, can this output be treated as an array instead of separated values? The matlab function block code is :
function [UB11,UB12,UB13,UB21,UB22,UB23,UB31,UB32,UB33,I] = fcn %coder.extrinsic('fcn') coder.extrinsic('xlsread'); persistent U; U=zeros(3,3); U=xlsread('C:\Users\Documents\MATLAB\InputImage.xlsx'); UB11=U(1,1); UB12=U(1,2); UB13=U(1,3); UB21=U(2,1); UB22=U(2,2); UB23=U(2,3); UB31=U(3,1); UB32=U(3,2); UB33=U(3,3); I=1; Thank you in advance, Sali

采纳的回答

Mike Hosea
Mike Hosea 2015-3-19
编辑:Mike Hosea 2015-3-19
The MATLAB Function Block can return arrays. The problem with your code is probably just that you can't return the persistent array. If you create an array UB = U and return UB instead of U, it should work.
As for the synchronization, there may be more direct ways of solving the problem that I don't know much about (tunable parameters?), but I think it should be possible to create a MATLAB function with persistent (or global) variables that acts as a repository. Something like
function [a,b,c] = myrepository(a,b,c)
persistent asaved bsaved csaved
if isempty(asaved)
asaved = 0;
bsaved = 0;
csaved = 0;
end
if nargin == 0
a = asaved;
b = bsaved;
c = csaved;
else
asaved = a;
bsaved = b;
csaved = c;
end
That's a MATLAB function that lives in MATLAB somewhere on the path, not in your MATLAB function block. Then in your MATLAB function block you retrieve the data
[a,b,c] = feval('myrepository');
and set the data
feval('myrepository',a,b,c);
or declare it extrinsic near the beginning of your function
coder.extrinsic('myrepository');
and get/set the data with
[a,b,c] = myrepository;
myrepository(a,b,c);
Of course you'll need to be sure to initialize the data in myrepository as appropriate before the simulation begins.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by