Convert String to instrument​.Oscillosc​opeObject

1 view (last 30 days)
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

Accepted Answer

Guillaume
Guillaume on 8 May 2017
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 Comments
Ayle
Ayle on 9 May 2017
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 on 9 May 2017
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).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!