You should be able to remove the line containing
if you are willing to use variable-sized arrays not constrained to double type.
If you do not mind variable-sized arrays if double type is inferred, the following may be a suitable solution.
function A = test
coder.extrinsic('xlsread');
persistent A_
if isempty(A_)
coder.varsize('A_');
A_ = zeros(1, 1);
A_ = xlsread('test.xlsx');
end
A = A_;
The generated code will contain uses of emxArray_real_T, both for the storage of A_ and as the result type of test.
If variable sizing must be completely removed, then the .xlsx file will need to be read as part of code generation (though it will still be reread when the generated MEX-function is first run). If you are building using the codegen command, this can be scripted.
test_size = size(xlsread('test.xlsx'));
save test.mat test_size;
codegen test;
However, test will need to be modified to load the test.mat file at compile-time.
function A = test
coder.extrinsic('xlsread');
persistent A_
if isempty(A_)
workspace = coder.load('test.mat');
A_ = zeros(workspace.test_size(1), workspace.test_size(2));
A_ = xlsread('test.xlsx');
end
A = A_;