Attempt to extract field 'Value' from 'mxArray'.

146 views (last 30 days)
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
  3 Comments
Balaji Subramanian
Balaji Subramanian on 18 Mar 2019
Hi Brian,
I have the same error too.
Currently, in my case, I am sending values from an integrator to a Data Store Block.
This Data store block is output of a PV Farm -> Signal converted from DC to AC.
However in the Master Code, it displays the 'mxArray' error.
Is it because I need to store the signal as an array ? Or make it global so the Array in Master code can directly access it instead of reading the whole PV Farm file ?
Thank you
Shray
Shray on 18 Jun 2023
Can you able to find answer to this question????

Sign in to comment.

Accepted Answer

Ryan Livingston
Ryan Livingston on 26 Mar 2014
Edited: Ryan Livingston on 26 Mar 2014
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.
  4 Comments
Brian
Brian on 27 Mar 2014
"When you say it gives a warning do you mean that it shows up underlined in yellow?"
Yes I do. My plan was to ignore it because it was still working with it, but if there was a fix for it, then I would have wanted to know it for this and future references.
And okay! That makes alot of sense. would you say its quicker to call the .m file rather than throw the code in the MATLAB Function Block? It probably would be easier for me to just create the .m file and call it, as you said, without worrying about alot of coder.extrinsic. etc.. But in terms of 100000 of iterations, would it make a difference?
Also, what about the S-Block? As long as I make the correct 'wrapper' functions for calling the .m file, would this be even faster? (or more code supported friendly?)
If you don't know, that's okay, they were just thoughts while reading your response. Thank you for all your help! I'll accept your answer as it did, with these comments, help answer my overall question!
Thanks again!
Ryan Livingston
Ryan Livingston on 27 Mar 2014
Sure thing. Generally, it may or may not improve performance by calling extrinsically. This depends upon how the function block code compares to what MATLAB is doing. If you have MATLAB code which is supported for code generation and that MATLAB code relies mostly on other MATLAB files, then the performance may be better to use the code in the function block.
However, if your code uses constructs which are unsupported for code generation, then calling it extrinsically is about the only recourse. I'm not too familiar with the S-Block workings, so I'll let others comment there.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown 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!