Not Enough Input Arguments
2 次查看(过去 30 天)
显示 更早的评论
I have imported a MATLAB code into a Python project in Visual Studio, using the MATLAB engine for Python. The MATLAB code generates a roughness profile for a surface and exports the information into an input file. The variables required for this process are Lx, Ly, Lz, N, sn_x, sn_y, WB_a, and WB_b. These variables control the size and roughness of the surface.
In MATLAB, these variables were entered in the command window, but in Visual Studio, I couldn’t find a similar command window, so I entered the variables as shown in the attached code. However, when I try to run the code, I keep getting a MatlabExecutionError related to missing input arguments on line 24 of the MATLAB file. Line 24 contains the following code: R_sample = -WB_a * gamma(1 + 1/WB_b) + WB_a * (-log(1-rand(sn_y, sn_x))).^(1/WB_b);.
I'm wondering if the way I'm entering the variables is incorrect, or how I can resolve this error. Any suggestions are appreciated.
import matlab.engine
eng = matlab.engine.start_matlab()
Lx = 1
Ly = 1
Lz = 0.2
N = 5
sn_x = 30
sn_y = 30
WB_a = 0.05
WB_b = 2
eng.workspace['Lx'] = Lx
eng.workspace['Ly'] = Ly
eng.workspace['Lz'] = Lz
eng.workspace['N'] = N
eng.workspace['sn_x'] = int(sn_x)
eng.workspace['sn_y'] = int(sn_y)
eng.workspace['WB_a'] = WB_a
eng.workspace['WB_b'] = WB_b
eng.Rand_surf(narfout=0)
eng.quit()
2 个评论
Steven Lord
2024-9-4
This line looks like a probably typo. Are you sure it wasn't supposed to be nargout instead of narfout?
eng.Rand_surf(narfout=0)
Without seeing the body of the MATLAB function you're trying to call, it's going to be difficult if not impossible to offer any concrete guidance.
回答(1 个)
Kanishk
2024-9-5
Hi,
Adding on to Steven’s comment, “narfout” is indeed a typo which should be “nargout”. In addition to this there is also another issue with the usage of “gamma” function in MATLAB script.
The “gamma” function is used which supports input types of “double” and “single”. However, the variable “W_b” in the Python script is initialized as an integer, resulting in an “int64” data type in MATLAB, which is not compatible with the “gamma” function. This results in the error :
matlab.engine.MatlabExecutionError
Undefined function 'gamma' for input arguments of type 'int64'.
To resolve this issue, you can either use “matlab.double” to explicitly convert the variable or initialize “W_b” as a float in Python, which will result in a double type in MATLAB.
WB_b = matlab.double(2)
or
WB_b = 2.0
Please go through the official MATLAB documentation of passing data types from Python to MATLAB to know more about data type mapping.
- https://www.mathworks.com/help/releases/R2023a/matlab/matlab_external/pass-data-to-matlab-from-python.html
- https://www.mathworks.com/help/releases/R2023a/matlab/ref/gamma.html
Hope this helps!
Thanks
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!