I am currently using this preliminary code:
static void Main(string[] args)
{
try
{
Type matlabtype;
matlabtype = Type.GetTypeFromProgID("matlab.application");
object matlab;
matlab = Activator.CreateInstance(matlabtype);
Execute(matlabtype, matlab, "clear;");
Execute(matlabtype, matlab, "path(path,'H:/bla/bla');");
Execute(matlabtype, matlab, "Object = ClassName();");
Execute(matlabtype, matlab, "Object.parameter1 = 100;");
Execute(matlabtype, matlab, "Object.parameter2 = 300;");
object o = Execute(matlabtype, matlab, "Object.ComputeSomething()");
}
catch (Exception e)
{
}
}
to create an object of a particular class, set some properties and compute something. Here:
ComputeSomething();
returns a scalar.
I am just wondering whether this is the best way to program this and what’s the cleanest way to obtain the actual scalar value without using string operations (e.g. remove ans =)?
Thanks.
Christian
You can retrieve data from matlab using a few commands. To get a scalar you can call GetVariable.
Execute(matlabtype, matlab, "result = Object.ComputeSomething()");
GetVariable(matlabtype, matlab, "result", "base")
see Call MATLAB COM Automation Server for available calls.
Related
I tried to use Matlab function from my .net project
function [HR] = getHR(signal)
Fs = 200;
index = pan_tompkin(signal,Fs);
index = index(2:end-1);
idx_dur = (index(end) - index(1))/Fs;
idx_cnt = length(index)-1;
idx_int = idx_dur/idx_cnt;
HR = round(60/idx_int);
end
This is my Matlab code
MLApp.MLApp matlab = new MLApp.MLApp();
matlab.Execute(#"cd C:\Users\User\Desktop");
object result = null;
matlab.Feval("getHR", 1, out result,input_Data);
object[] res = result as object[];
tmp_HR = Convert.ToInt32(res[0]);
And this is part of my .net code where calling Matlab function
input_Data is 2000x1 double array
When I run this program, error is occur that "Undefined function 'getHR' for input arguments of type 'double'." on Matlab.Feval line
Someone advised me to 'varargin' to solve this problem, but I can not find the answer(I don't think it is necessary)
How can I revise my code to fix this problem?
I can not use the way to use Matlab compiler SDK or Matlab coder
maybe have to use only MLApp to solve this problem
I use a nuget package called Silk.NET to use DirectX12 in C#. For example, I can create an IDXGIFactory5 with the following code:
var dxgi = DXGI.GetApi();
var d3d12 = D3D12.GetApi();
var factoryFlags = 0u;
var factoryiid = IDXGIFactory5.Guid;
IDXGIFactory5* factory = null;
dxgi.CreateDXGIFactory2(factoryFlags, ref factoryiid, (void**)&factory);
But for some unavoidable reasons, I have to create the IDXGIFactory5 COM interface in C++:
ComPtr<IDXGIFactory5> factory;
CreateDXGIFactory2(0, IID_PPV_ARGS(&factory));
How can I convert the COM interface created in C++ to the type in Silk.NET?
(Because I am not good at English, I am using Google Translate to ask questions, please forgive me)
I am trying to create a C# program that lets user's provide an implementation for for a function by inputting text into a text box. I provide the function header (input types, output type), they just need to provide actual implementation. I then store that function to call later. They might need to import something from the .NET framework, but nothing outside of it.
I don't care about security, this is just for a tool for internal use.
Is there an easy way to do this in .NET?
The usage would look something like (need to implement the CompileUserFunction function, which takes in an int and returns an object):
Func<int, object> CreateUserFunction(string input) {
Func<int, object> userFunc = CompileUserFunction(input);
return (i) => userFunc(i);
}
public void DoSomething() {
List<Func<int, object>> userFuncs = new List<Func<int, object>>();
string userInput = #"DateTime t = DateTime.Now;
t.AddDays(i);
return t;";
userFuncs.Add(CreateUserFunction(userInput));
userFuncs.Add(CreateUserFunction("return i;"));
userFuncs.Add(CreateUserFunction("i = i * 5; return i;"));
var result = userFuncs[0](5);
}
You can use code generation libs for that task.
I advice you to use Roslyn scripting API. I have done a similar task - parsing a string into delegate with it. The following example is taken from this link: https://blogs.msdn.microsoft.com/csharpfaq/2011/12/02/introduction-to-the-roslyn-scripting-api/
You will find there more examples
using Roslyn.Scripting.CSharp;
namespace RoslynScriptingDemo
{
class Program
{
static void Main(string[] args)
{
var engine = new ScriptEngine();
engine.Execute(#"System.Console.WriteLine(""Hello Roslyn"");");
}
}
}
There are other code generation tools and libs:
CodeDom - an old .Net code generation Framework. Probably can be used here but is more tricky.
https://learn.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/using-the-codedom
There were some libraries which were used to convert strings to Linq Expression trees, but it all seems to be outdated now.
There is also a possibility to create a Dynamic Method via Reflection.Emit but it is very low level - you need to define method implementation in IL instructions.
I have some code written in Matlab however I wish to call this code from a C# console application.
I do not require any data to be returned from Matlab to my app (although if easy would be nice to see).
There appears to be a few options however not sure which is best. Speed is not important as this will be an automated task.
MATLAB has a .Net interface that's well-documented. What you need to do is covered in the Call MATLAB Function from C# Client article.
For a simple MATLAB function, say:
function [x,y] = myfunc(a,b,c)
x = a + b;
y = sprintf('Hello %s',c);
..it boils down to creating an MLApp and invoking the Feval method:
class Program
{
static void Main(string[] args)
{
// Create the MATLAB instance
MLApp.MLApp matlab = new MLApp.MLApp();
// Change to the directory where the function is located
matlab.Execute(#"cd c:\temp\example");
// Define the output
object result = null;
// Call the MATLAB function myfunc
matlab.Feval("myfunc", 2, out result, 3.14, 42.0, "world");
// Display result
object[] res = result as object[];
Console.WriteLine(res[0]);
Console.WriteLine(res[1]);
Console.ReadLine();
}
}
i am using the arcGIS api to make a plugin for arcFM, when i try to run this code
Type t = Type.GetTypeFromProgID("esriFramework.AppRef");
System.Object obj = Activator.CreateInstance(t);
pApp = obj as IApplication;
i get
System.Runtime.InteropServices.COMException(0x8000FFFF): Creating an instance of the component with CLSID {Appref CLSID HERE} from the IClassFactory faileddue to the following error: 8000ffff
Thanks
This was impossible i needed to be using arcMap not ArcFM
In the AppRef CoClass documentation, it says:
Note you can only use the AppRef
object if your code is running inside
one of the ArcGIS application
processes.
Forum posts seem to confirm that this is the same error which is seen when this constraint has been violated:
From http://forums.esri.com/Thread.asp?c=93&f=1729&t=217861:
It is my understanding that there is
indeed no way to access the
IApplication instance from a
geoprocessing script.
In theory, if your task is purely
geoprocessing, you should be able to
do it all without accessing the
IApplication object.
It looks like the OP of the above forum post was able to get around their problem by "using IToolboxWorkspace and accessing directely the Esri-toolboxes". This was her code:
public IGPTool GetTool(string _sToolName, string _sToolboxName)
{
IWorkspaceFactory pGPTFact;
IToolboxWorkspace pToolboxWorkspace;
IGPToolbox pGPToolbox;
IGPTool pGPTool;
pGPTFact = new ToolboxWorkspaceFactoryClass();
pToolboxWorkspace = pGPTFact.OpenFromFile(
ArcGISInstallFolder + #"\ArcToolbox\Toolboxes", 0) as IToolboxWorkspace;
pGPToolbox = pToolboxWorkspace.OpenToolbox(_sToolboxName);
pGPTool = pGPToolbox.OpenTool(_sToolName);
return pGPTool;
}
private string ArcGISInstallFolder
{
get
{
if (string.IsNullOrEmpty(this.m_sArcGISInstallFolder))
{
Microsoft.Win32.RegistryKey regkey;
regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
#"Software\ESRI\ArcGIS", false);
this.m_sArcGISInstallFolder = regkey.GetValue("InstallDir") as String;
}
return this.m_sArcGISInstallFolder;
}
}
Perhaps you can accomplish your goal either without the AppRef object or by running your script from inside the application.