Simulink Error: MATLAB expression '<output of pyrunfile>' is not numeric.

4 Ansichten (letzte 30 Tage)
Hello! I have been trying to implement a Matlab block in Simulink. The code is rather simple as of now:
function p = pythoncalltest(inputArg1, inputArg2)
%PYTHONCALLTEST Summary of this function goes here
% Detailed explanation goes here
x = zeros(1, 10);
coder.extrinsic("vpa");
coder.extrinsic("pyrunfile");
x = pyrunfile("testpy.py", "outputArg1", t = inputArg1, a = inputArg2);
x = vpa(x);
p = double(x);
end
I want to input an array and a constant (this works). Then I want to output it (this doesn't work). It always tells me either
"MATLAB expression '<output of pyrunfile>' is not numeric." Or - if I don't use the "zeros(1, 10)" first, that it can't output an MxArray.
Here is my Python Code:
def add(inputArg1, a):
out = []
for i in inputArg1:
out.append(float(i + a))
return out;
outputArg1 = add(t, a)
I really don't know what to do. Please help! Thank you!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 11 Mai 2023
?? What data type are you expecting to get back from pyrunfile, such that you want to call vpa() on it and then call double() on the result of that?
vpa() is well defined for a few different input types:
  • numeric (any of the built-in numeric classes), in which case entries are evaluated to symbolic floating point numbers
  • logical, in which case entries are evaluated to symbolic floating point numbers 0.0 or 1.0
  • symbolic
  • symbolic function
  • character vector or string scalar or string array, in which entries are numeric literals, in which case the entries are evaluated to symbolic floating point numbers
  • character vector or string scalar or string array, in which entries are valid MATLAB variable names, in which case references to symbolic variable names are returned
  • cell array whose entries are any of the above
None of these are double precision.
At this point, there are two logical possibilities:
  • because of the previous initialization of x as double precision, that Simulink attempts to convert the symbolic or symfun result to double precision. If the input was representable as a symbolic floating point number that could potentially succeed, with double precision output; if the input was not representable as a symbolic floating point number then conversion would fail, leading to an error; or
  • perhaps Simulink will just complain that the datatypes do not match, leading to an error
So the processing of the vpa() call is going to give an error if the input is invalid, or if it is considered a type mismatch, or if it could not be evaluated to symbolic floating point number; if it succeeds at all, the result would already be double precision, with there not needing to be any need to double() the result.
Can the result be symbolic? Well, the inputs to pythoncalltest are Simulink signals, and Simulink signals cannot be symbolic or symfun or character vector or cell array -- but in sufficiently new Simulink could potentally be string() entries.
If the results of pyrunonfile are numeric then it is a waste of time to call vpa() on them: just call double() directly. (Except you don't need to do that.)
If the results of pyrunonfile are string() then the x = pyrunfile will either give an error or will already convert them to double precision already.
So the vpa() and following double() can only cause problems, not solve any problems.
  1 Kommentar
Oliver
Oliver am 15 Mai 2023
Thank you for all your help! The thing I did now, which worked, is writing the function in a Matlab file and then calling this matlab function from Simulink. Yes, vpa() was a waste, I just wanted to try everything and it didn't hurt.
It is really annoying me how Simulink and Matlab code works so differently.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by