combining matlab code to simulink

4 Ansichten (letzte 30 Tage)
Nkenna Amadi
Nkenna Amadi am 28 Jul. 2018
Beantwortet: Aquatris am 30 Jul. 2018
hi i have the following code. i want to be able to combine it with a simulink model, where the simulink model would provide a source to be connected to this code, the value from the source would be the variable called HCEx(which is already defined in the code as a random number) but i want to replace this value with a value from the simulink model.
Lx = 51; % Defines the size of the lattice .
Ly = 51;
HCEx=120;
pt =HC7;
IM = zeros(Ly,Lx); % Initialization of the lattice
% IM(i,j)=2
nstop=0;
t =1;
tend =1; % Number of "time steps".
k = 1; % Counting index for front propagation .
q = 1; % Counting index for image writing .
% Initial step. The invader starts from a "point" or a channel
%IM(Ly,50)=1; % Point IP
IM(Ly,10)=1; % Channel IP
% Finds the indices of the largest element in a given row in pt and invades (=1) the corresponding element in IM
%Defining the Trap spill point
while tend==1 && nstop==0
% Check if there are trapped clusters
IM=bwlabel(IM-1,4); % Labels defending clusters , by setting invader sites = 0.
IM=IM+1; % Keeps the labeling , and puts the invader sites = 1.
for i = 1:Ly
s = sort(IM(i,1:end)); % Sorts the ith row in ascending order.
if s(1)==1 % Tests if the ith row contains an invader site .
for j = 1:Lx
bin = front(i-1,j, IM,Lx,Ly); % If the ith row contains an invader site , all the elements in the ith row are being tested in the function front to see if they are part of the front .
if bin==1
% pfront is the front matrix where the first column
% contains the probability and the second and third
% columns contain the corresponding indices for this front site.
pfront(k,1) = pt(i-1,j);
pfront(k,2) = i-1;
pfront(k,3) = j;
k = k+1;
end
end
end
end
% Probability y-coordinate xcoordinate.
% Finds the row index of the smallest probability in pfront and invades (=1) the corresponding element in IM
e = find(pfront(:,1)==min(pfront(:,1)));
IM(pfront(e,2),pfront(e,3)) = 1;
clear pfront
k = 1;
% Put invader sites = 1, and defending sites = 0.
IM(IM~= 1)=0;
% The following lines saves the image for each iteration .
imagesc(IM),colorbar,drawnow
% The following lines saves the image for each iteration.
Vol=numel(IM==1);
if Vol>HCEx
nstop=1
end
end
%Computes the total volume
function [bin] = front(a,b,IM,Lx,Ly)
% The site is part of the front if one of the neighboring sites is an invader.
if IM(a,b)~=IM(1,1) % Takes trapped clusters into account If the site % tested is not part of the large defending cluster it should not be counted as a front site .
bin=0;
elseif (b+1)==(Lx+1)
if (IM(a+1,b)==1)||(IM(a-1,b)==1)||(IM(a,b-1)==1)
bin = 1;
else
bin = 0;
end
elseif (b-1)==(1-1)
if (IM(a+1,b)==1)||(IM(a-1,b)==1)||(IM(a,b+1)==1)
bin = 1;
else
bin = 0;
end
elseif (a+1)==(Ly+1)
if (IM(a-1,b)==1)||(IM(a,b+1)==1)||(IM(a,b-1)==1)
bin = 1;
else
bin = 0;
end
elseif (a-1)==(1-1)
if (IM(a+1,b)==1)||(IM(a,b+1)==1)||(IM(a,b-1)==1)
bin = 1;
else
bin = 0;
end
else
if (IM(a+1,b)==1)||(IM(a-1,b)==1)||(IM(a,b+1)==1)||(IM(a,b-1)==1)
bin = 1;
else
bin = 0;
end
end
end

Antworten (1)

Aquatris
Aquatris am 30 Jul. 2018
You are looking for " Matlab Function " block.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by