Matlab Function In Simulink
6 views (last 30 days)
Show older comments
Hi all,
So I wrote some matlab code which works perfecting. I am now trying to add this code as a function block in simulink, however I am getting some errors. Mainly that the 'readtable' function does not work anymore! Anny suggestions??
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CODE:
function y = fcn
%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;![FSK_Simulation_Error.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/262994/image.jpeg)
![FSK_Simulation_Error.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/262994/image.jpeg)
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
%Transmitt the frame, one element at a time
for u=1:length(frame)
y=frame(u);
end
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
These are the errors:
Function 'readtable' not supported for code generation. Function 'MATLAB Function' (#51.84.107), line 6, column 3: "readtable('Depart.csv')" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 't'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#51.123.124), line 7, column 15: "t" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'A'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#51.165.166), line 10, column 13: "A" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'index'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#51.561.566), line 25, column 11: "index" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'A'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#51.669.670), line 28, column 23: "A" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'A'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#51.705.706), line 29, column 17: "A" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'y'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#51.745.746), line 30, column 26: "y" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'y_shift'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#51.768.775), line 31, column 17: "y_shift" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'x'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#51.796.797), line 32, column 15: "x" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'final'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#51.920.925), line 36, column 24: "final" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'final'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#51.1026.1031), line 40, column 32: "final" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'A'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#51.1177.1178), line 47, column 21: "A" Launch diagnostic report.
0 Comments
Answers (1)
Kaashyap Pappu
on 23 Jan 2020
This problem can be attributed to readtable not supporting code generation. The MATLAB Function Block first generates code for the script, then executes it in the Simulink environment.
You can try to use From Spreadsheet block to read the .CSV file. This could require additional changes to the logic but could serve as a workaround regardless. The From Spreadsheet block needs a stime stamp and data value, which seems to be how your data is structured, so should work. Adjust simulation time based on the time stamps in your data and pass 'X' and 'Y' data to the function call as input arguments.
Hope this helps!
0 Comments
See Also
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!