필터 지우기
필터 지우기

Access arbitrary class from MATLAB Engine interface

조회 수: 2 (최근 30일)
Szabolcs
Szabolcs 2013년 2월 11일
Using the MATLAB Engine interface, it is possible to access most data types such as numerical arrays, cells, structs, etc.
Is it also possible to access arbitrary classes such as an MException? If so, how? Just access the fields using mxGetField() etc. as in the case of structs? I am interested in MException in particular.
EDIT: I tried this, but unfortunately MATLAB (not the engine program, but the MATLAB process) crashes whenever I tried to call engGetVariable() on a variable of class MException. I'm on OS X. I would appreciate any suggestions about possible workarounds. Note that because of this crash, it is simply not safe to pass arbitrary variable names to engGetVariable(). There's always a chance it would crash MATLAB.

채택된 답변

Amro
Amro 2013년 2월 19일
A trick is to convert any OOP class object to a regular structure in the MATLAB side:
ME = MException('a:b','err');
s = struct(ME);
Note that a warning is issued that any private, protected, or hidden properties of the object will become public fields in the new structure:
>> s
s =
type: {[0] ''}
hasBeenCaught: 0
identifier: 'a:b'
message: 'err'
cause: {}
stack: [0x1 struct]
defaultstack: [0x1 struct]
arguments: {}
Now you can retrieve the new variable using the Engine API, and handle it as a regular structure:
mxArray *s = engGetVariable(ep, "s");
mxArray *identifier = mxGetField(s, 0, "identifier");
mxArray *message = mxGetField(s, 0, "message");
  댓글 수: 2
Szabolcs
Szabolcs 2013년 2월 19일
Thanks for the answer! One missing piece is that I'd actually need to recurse deep on any struct/cell/class and convert every class within to structs. The crash would happen if I try to engGetVariable a struct or cell which contains a class arbitrarily deep inside.
Amro
Amro 2013년 2월 19일
ah, hadn't thought of that.. I guess you can traverse the structure fields recursively and use isobject() to check if its an class object or not (in the MATLAB side of course)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jing
Jing 2013년 2월 19일
First, I'm not familiar with MATLAB Engine. If I misunderstand your question, just ignore me.
The MException object is accessible in MATLAB program. It's an object, so you can access it's property like other class. It's similar to structs. For example, you have a MException object ME, just type ME.message in the command window, you can get the message out of it.
  댓글 수: 1
Szabolcs
Szabolcs 2013년 2월 19일
편집: Szabolcs 2013년 2월 19일
I'm afraid you misunderstood the question. This is about about accessing the class from the C language, not from MATLAB.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by