Using C # method / property in MATLAB try / catch statement

I have developed a driver for an automatic microscopic system written in C # and compiled using Visual Studio 2013. I have a particular problem when assigning some properties in a try

/ loop catch

.

Below is a sample MATLAB code (simplified for display).

pwsAssembly = NET.addAssembly ( 'file location' );
scope = Pws . Scope ( );
scope . Connect();

% This command performs the movement.
scope . Objective = 1;

% This command DOES NOT perform the movement, but DOES NOT enter the catch statement.
try
  scope . Objective = 4;
catch
  error ( 'Unable to adjust objective' );
end

% Again, this command performs the movement:
scope . Objective = 4;

      

Objective

is the property Get

/ Set

in the class Scope

.

Any ideas on why a Set

C # property will not work correctly in a MATLAB try

/ statement catch

?

ADDITIONAL INFORMATION

I have also characterized the behavior in MATLAB.

  • If I execute the code as a script, with no breaks or pauses, some operations are skipped.
  • If I "step" through the script, each line is executed as intended, even in the try

    / operator catch

    .
  • If the property assignment is inside a statement if

    , it always succeeds.

The following MATLAB code has been modified to reflect this observation.

pwsAssembly = NET . addAssembly ( 'fileLocation' );
scope = Pws . Scope ( );
scope . Connect ( );

scope . Objective = 1; % Unsuccessful. Successful if I "step" through.

try
  scope . Objective = 4; % Unsuccessful . Successful if I "step" through.
catch
  error ( 'Headaches' );
end

if ( scope . Objective  ~= 6 )
  scope . Objective = 6; % Successful, always.
end

      

Any thoughts?

+3


source to share


1 answer


I've tried with a small build, but I can't reproduce the problem:

Scope.cs

using System;
namespace PWS
{
    public class Scope
    {
        public int Objective { get; set; }

        public Scope()
        {
            Objective = 0;
        }

        public void Connect()
        {
            Console.WriteLine("connected");
        }
    }
}

      

Compiled into assembly using:

C:\> csc.exe /target:library Scope.cs

      



MATLAB

Here is the code for use in MATLAB:

>> NET.addAssembly(fullfile(pwd,'Scope.dll'));

>> scope = PWS.Scope();
>> scope.Connect();

>> scope.Objective = 1;
>> try, scope.Objective = 4, catch ME, error('failed'); end
>> if (scope.Objective ~= 6), scope.Objective = 6; end

      

All lines ran fine no matter how I run the code: executed interactively in the command window, run as either a script, as usual, or when stepping through the code in the debugger.

(Note: any calls Console.WriteLine

usually don't show up in MATLAB, although there are ways to capture the output from .NET )

+1


source







All Articles