Convert String to instrument​.Oscillosc​opeObject

1 次查看(过去 30 天)
I am using MATLAB function in C#. I am trying to get back the value of myScope in C# so that I can use it later on, but the type of myScope is instrument.Oscilloscope which is not recognized by C# and it displays 'null' in result. If I send back the myScope value to C# as string (which I can do) but late on (in code) I have to send it back to Matlab and obviously as "instrument.Oscilloscope" . Is there some way of converting string to instrument.Oscilloscope.
// Create the MATLAB instance
MLApp.MLApp matlab = new MLApp.MLApp();
// Change to the directory where the function is located
matlab.Execute(@"cd c:\MATLABcodevisualstudio");
// Define the output
object result = null;
try {
// Call the MATLAB function myfunc
matlab.Feval("ConfigureScope", 1, out result, "2", "100", "USB::0x0699::0x0367::C057105::INSTR", "CH1", "CH2", "normal", "Falling", "1"); //working
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.GetType().FullName);
Console.WriteLine(ex.Message);
}
// Display result
object[] res = result as object[];
%%%%%%%%%%%%%%%%%%Matlab func
function [myScope]= ConfigureScope(ResourceAddress, AcquiringChannel, TriggeringChannel,TriggerMode, TriggerSlope, TriggerLevel)
% Instantiate an instance of the scope.
myScope = oscilloscope();
% Find resources.
availableResources = getResources(myScope);
%connect to availble resources
myScope.Resource = ResourceAddress;
% Connect to the scope.
connect(myScope);
% Enable channel 1.
enableChannel(myScope, AcquiringChannel);
% Set the trigger mode to normal.
set(myScope, 'TriggerMode', TriggerMode);
set(myScope, 'TriggerSlope', TriggerSlope);
set(myScope, 'TriggerSource', TriggeringChannel);
set(myScope, 'TriggerLevel', str2double(TriggerLevel));
myScope

采纳的回答

Guillaume
Guillaume 2017-5-8
I'm not sure you can pass objects through matlab COM interface. Why don't you use Execute instead of Feval to create the oscilloscope in the matlab workspace. You can then use more Execute to manipulate that oscilloscope and GetVariable or GetFullMatrix to get at the variables of interest.
  5 个评论
Ayle
Ayle 2017-5-9
That solved my problem:) thanks One more question; using this matlab.execute works sequentially with c# code or is the command running in parallel in Matlab?
Guillaume
Guillaume 2017-5-9
Execute waits until the command is completed since it must collect its output (if any). So it is sequential (unless you specifically design the matlab command to be asynchronous which would require the parallel processing toolbox or some fancy delegating to Java, mex, or .Net from the matlab side).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from .NET 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by