Clear Filters
Clear Filters

Cannot Use MATLAB Coder to Generate C

3 views (last 30 days)
Wei Yu Chen
Wei Yu Chen on 19 Jul 2022
Answered: Nathan S on 21 Jul 2022
I would like to generate c code from my function.
It's a function about reading data from .csv file generated by a sensor.
Below is my function code:
function [data_file, Sample_Rate, head, tail] = read(data_source_name)
data_file = readmatrix(data_source_name);
filen2 = char(data_source_name);
filen = [filen2];
files1 = [filen '.csv'];
textstring = textread(files1,'%s',16);
naxsenid = textstring(2);
recordd = textstring(6);
recordt = textstring(7);
comnm = textstring(8);
comns = comnm{1,1};
comma = strtok(comns,',');
if ( comma == "")
sri = 16;
else
sri = 15;
end
srf = split(textstring(sri),'-');
sdata = split(textstring(sri-4),',');
srt = textscan(srf{1,1},'%f%c');
datans = textscan(sdata{1,1},'%f');
if (srt{1,2} == 'K')
Sample_Rate = srt{1,1}*1000;
else
Sample_Rate = srt{1,1};
end
datanc=datans{1,1};
fprintf('naxsen_id = %s\n',naxsenid{1,1});
fprintf('record_date = %s\n',recordd{1,1});
fprintf('record_time = %s\n',recordt{1,1});
fprintf('total_data_number = %d\n',datans{1,1});
fprintf('Sample_Rate = %f\n',Sample_Rate);
start = (1/Sample_Rate)*Sample_Rate-1+5;
stop = start+(datanc-1);
stop_time = (stop-5+1)/Sample_Rate;
fprintf('%f\n',stop_time);
datan = 1:(stop-start+1);
t = datan/Sample_Rate;
Array = csvread(files1,start,0,[start,0,stop,5]);
f = Sample_Rate*((-datanc/2):(datanc/2)-1)/datanc;
data_length = size(Array);
classn = data_length(1,2);
head = 1;
tail = data_length(1,1);
end
and test code be like
data_source_name = "test";
[data_file, Sample_Rate, head, tail] = read(data_source_name);
the content of "test" is
### NAXSEN800387 Data0
### Time 2021/09/08 16:26
### Data Length 9
### Sample Rate 100-Hz
accX,accY,accZ,gyroX,gyroY,gyroZ
1.01611328125,0.00927734375,0.0244140625,-0.3662109375,0.732421875,0.3662109375
1.0166015625,0.00830078125,0.0224609375,-0.30517578125,0.732421875,0.48828125
1.017578125,0.01025390625,0.02490234375,-0.3662109375,0.732421875,0.48828125
1.01513671875,0.01171875,0.02294921875,-0.30517578125,0.79345703125,0.48828125
1.0146484375,0.01025390625,0.025390625,-0.3662109375,0.79345703125,0.48828125
1.01513671875,0.0087890625,0.0244140625,-0.3662109375,0.79345703125,0.48828125
1.0185546875,0.00732421875,0.0224609375,-0.30517578125,0.732421875,0.3662109375
1.01611328125,0.00927734375,0.0244140625,-0.30517578125,0.732421875,0.42724609375
1.01513671875,0.0087890625,0.02587890625,-0.3662109375,0.732421875,0.42724609375
Output of test code
naxsen_id = NAXSEN800387
record_date = 2021/09/08
record_time = 16:26
total_data_number = 9
Sample_Rate = 100.000000
0.090000
the last row "0.090000" means execution time, due to 9 data divided by sample rate 100 Hz = 0.09
When I executed MATLAB Coder, I got some error information below
I have no idea how to instead these function like readmatrix, split, etc.
Is there any way to resolve this issue?
If so, could you provide clear instructions/examples using my code?
Thanks a lot!

Answers (1)

Nathan S
Nathan S on 21 Jul 2022
I think your best option is to use a combination of fscanf and fgetl, both of which are supported for code generation.
The best way to write such code will depend on what assumptions you can make about your file (which strings have the same length all the time, do you always have the same number of columns, are there likely to be any extra lines or extreneous data, etc.)
I have made some assumptions about what you might want, and this works on your example file in codegen for me. It will probably not be as robust to files that are slightly wrong as readmatrix would be.
function [data_file, Sample_Rate, head, tail] = readmyfile(data_source_name)
fid = fopen(data_source_name);
naxsenid = simpleSplit(fgetl(fid), 2);
[record_date, record_time] = simpleSplit(fgetl(fid), 3:4);
total_data_number = fscanfForScalar(fid, '### Data Length %d');
fgetl(fid); %skip any remaining whitespace / end of line
Sample_Rate = fscanfForScalar(fid, '### Sample Rate %f');
sample_modifier = fscanf(fid, '%c', 1);
fgetl(fid); %skip any remaining whitespace / end of line
if sample_modifier == 'K'
Sample_Rate = Sample_Rate * 1000;
end
headerLine = fgetl(fid);
numCols = sum(headerLine==',')+1;
fprintf('naxsen_id = %s\n',naxsenid);
fprintf('record_date = %s\n',record_date);
fprintf('record_time = %s\n',record_time);
fprintf('total_data_number = %f\n',total_data_number);
fprintf('Sample_Rate = %f\n',Sample_Rate);
data_file = zeros(total_data_number, numCols);
for r = 1:total_data_number
for c = 1:(numCols-1)
data_file(r,c) = fscanf(fid, '%f,', 1);
end
data_file(r,numCols) = fscanf(fid, '%f\n', 1);
end
head = 1;
tail = size(data_file, 1);
fclose(fid);
end
function [varargout] = simpleSplit(str, idx)
spaces = [0, find(str==' '), numel(str)+1];
spaces(diff(spaces)==1) = [];
coder.unroll()
for i = 1:numel(idx)
varargout{i} = strtrim(str((spaces(idx(i))+1):(spaces(idx(i)+1)-1))); %#ok<AGROW>
end
end
function out = fscanfForScalar(fid, format)
tmp = fscanf(fid, format, [1,1]);
if isempty(tmp)
out = 0;
else
out = tmp(1,1);
end
end

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!