MATLAB: Attempt to extract field ‘Value’ from ‘mxArray’.

actxservercomexcelmatlab codermxarraysimulinkvalue

In Simulink, Im trying to get the value from a cell in an Excel Spreadsheet, but everytime I try to run the model, I get this error…
Attempt to extract field 'Value' from 'mxArray'.
Why? and how do I fix it.
PS: The error comes from disp(iiTHEVALUE.Value); (right now I'm just displaying the value in the matlab command window, once I get it working, I will start to do things with it)
This is my code in a Matlab function box in Simulink…
function count_oute = Excel(counte)
%#codegen
%%Lets Simulink know to read commands as Matlab commands
%Keep at top
coder.extrinsic('pwd');
coder.extrinsic('strcat');
coder.extrinsic('actxserver');
coder.extrinsic('invoke');
coder.extrinsic('get');
coder.extrinsic('set');
coder.extrinsic('delete');
%%Global Variables?
%%Editable Variables
Excel_File = 'If_actxserver.xlsx';
%%Defining Variables
Current_Directory = pwd;
excel_filename = strcat(Current_Directory,'\',Excel_File);
%%Keeping COM.handles active through iterations in this specific function block
%Excel handle Variables
persistent h_Excel;
if isempty(h_Excel)
%if handle is empty, define (which it is at the start of a model)

h_Excel = actxserver('Excel.Application');
end
persistent Excel_Workbook;
if isempty(Excel_Workbook)
%if handle is empty, define (which it is at the start of a model)
Excel_Workbook = invoke(get(h_Excel, 'Workbook'), 'Open',excel_filename);
end
%%If first iteration
if (counte==1)
set(h_Excel, 'Visible', 1);
disp('Started Mathcad application');
end
%%Iterate on Excel
disp('Excel Iterate #');
disp(counte);
%%Save, Close, and Quit actxservers on last iteration
if (counte==11)
iTHEVALUE = invoke(get(Excel_Workbook, 'Worksheets'),'Item','InitialVars');
iiTHEVALUE = invoke(iTHEVALUE, 'Range','A1:B4');
disp(iiTHEVALUE.Value);
invoke(Excel_Workbook,'Close');
invoke(h_Excel,'Quit');
invoke(h_Excel,'delete');
disp('Excel Closed');
end
%Out value is the same as the in value, just displaying again
count_oute=counte;
end

Best Answer

When using the output of an extrinsic function, it may often be necessary to preinitialize the variable to which it is assigned. Doing so is what tells the code generation software about the type, size and complexity of the value returned. If foo is extrinsic, then you could use:
x = zeros(3,4);
x = foo(y);
to call foo extrinsically and assign the output as a 3-by-4 array. See:
for more details.
However, in your case, the output of invoke is likely not going to be a type supported for code generation as it is likely an ActiveX or COM object. When that is the case I find it easier to put all of the unsupported functionality into a single function and then just call that function extrinsically.
So in your case, you could write a separate MATLAB function that takes counte and the file name as arguments and returns the data as a numeric array. Then you can use the preinitialization technique above when calling your new function to let Simulink know what type it is.