error in neural network for c#(.net Assemly)

4 次查看(过去 30 天)
Hello,
I always got same error all the time. I already no ideal what is the misdake i made.
C# error information:
... MWMCR::EvaluateFunction error ...
The 'STRING' input must be either a char row vector, a cell array of char row vectors, or a string array.
Error in => Mynntest.m at line 7.
Would anyone provide a direction for me ?
---------------------------------
My script :
I compile my script for C#(.net Assemly). (storage path:C:\\ANN )
My C#:
Error information in C#:

回答(1 个)

Nihal
Nihal 2024-7-26
The error message you're encountering indicates that a function in your MATLAB code expects a string input in a specific format, but it is not receiving it in the correct format. This is a common issue when interfacing MATLAB code with C# via the MATLAB Compiler Runtime (MCR).Steps to Troubleshoot and Resolve the Issue
Check the Input Type:
  • Ensure that the input you are passing from C# to MATLAB is in the correct format. The error message specifies that the input must be a char row vector, a cell array of char row vectors, or a string array.
Review the MATLAB Function:
  • Open the MATLAB function Mynntest.m and look at line 7. Identify the variable that is causing the error and ensure it is being passed the correct type of string input.
Convert Input in C#:
  • Ensure that the string you are passing from C# is properly converted to a format that MATLAB can understand.
Example C# Code for Correct String Formatting
Here’s an example of how you can pass a string from C# to MATLAB using the MATLAB Compiler Runtime:
csharp
using MathWorks.MATLAB.NET.Arrays; // Ensure you have this namespace
public void CallMatlabFunction()
{
// Example string input
string inputString = "example input";
// Convert the C# string to a MATLAB string
MWCharArray matlabString = new MWCharArray(inputString);
try
{
// Assuming you have a compiled MATLAB function called 'Mynntest'
MynntestClass myMatlabFunction = new MynntestClass();
// Call the MATLAB function with the properly formatted string
myMatlabFunction.Mynntest(matlabString);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
Example MATLAB Function
Here’s an example of what your MATLAB function might look like:
function output = Mynntest(inputString)
% Ensure the input is in the correct format
if ischar(inputString) || iscellstr(inputString) || isstring(inputString)
% Your function logic here
disp(inputString);
output = 'Success';
else
error('The ''STRING'' input must be either a char row vector, a cell array of char row vectors, or a string array.');
end
end
Summary
  • Ensure Input Type: Verify that the input being passed from C# to MATLAB is in the correct format.
  • Review MATLAB Code: Check the MATLAB function for the expected input format.
  • Convert Input in C#: Use MWCharArray to convert C# strings to MATLAB-compatible strings.
By ensuring that the input type matches the expected format in MATLAB, you should be able to resolve the error.

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler SDK 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by