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