Simulink implementation of Matlab Code

1 view (last 30 days)
Andrea Carroll
Andrea Carroll on 27 Jan 2020
Answered: Samatha Aleti on 30 Jan 2020
I am trying to implement the following frame generation code in simulink. In summary the code does the following:
  • Reads excel spreadsheet (which contains two columns, 1)time stap, 2)flight number)
  • Starts a cycle, using the while statement, this is to mimic the passage of time
  • Converts the two inputs into binary, concatenates them together and adds them to a frame (array)
  • Every 10 cycles a new frame is created into which more inputs are add
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%555
clc;
%Initialisation of variables
x_count=1;
y_count=1;
frame=[];
t=readtable('Depart.csv');
A=table2array(t);
cycle_count=1;
start=610;
index=int64(A(x_count,1));
frame_index=1;
filename='Write_To.csv';
%this is the time cycle which runs from 6am to 11:30pm and increases by one
%minute every cycle. Starting at 601 to allign with cycle_count (which
%starts at 1 as well)
for time=601:2330
time;
cycle_count;
%The reason for using a while loop here is because multiple
%flights occure at the stame time.
while index<=time
%Table extraction (bit containting: time and flight no.)
x=de2bi(int64(A(x_count,1)), 24);
y=int64(A(x_count,2));
y_shift=bitshift(y,12);
d=de2bi(y_shift, 24);
final=x+d;
%adds the binary values of the x and y data to the frame
%sequencially
for i=1:length(final)
%adds single bit to the frame
frame(frame_index)=final(i);
frame_index=frame_index+1;
end
%Update variables
x_count=x_count+1;
index=int64(A(x_count,1));
end
%If the count has reached 10 we are going to reset the frame to
%be empty
if cycle_count==10
time
disp('Printing frame at end of cycle');
frame
disp('Resetting cycle count');
frame=[];
frame_index=1;
end
if cycle_count==10
cycle_count=1;
else
cycle_count=cycle_count+1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The trouble that I am having with this is that I cannot simply copy and paste this code into a matlab function block in simulink.
The main problem at the moment is how to store multiple values in a frame in simulink, not just the latest input. Would implementing this using a Matlab System block in simulink work? If so how would I write it?
Hopefully this makes sense.
Thanks

Answers (1)

Samatha Aleti
Samatha Aleti on 30 Jan 2020
According to my understanding you want to store the output value “final” at each time step in an array instead of just saving the recent value, inside a MATLAB block function. One of the ways to do this is to define “frame” as persistent in the MATLAB block function as follows:
function [count] = fcn(in, count)
count = count + 1;
persistent frame
if isempty(frame)
frame = in;
else
frame = [frame, in];
end
% reset
if count == 10
count = 1;
frame = [];
end
end
Hope this helps!

Categories

Find more on General Applications in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!