Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Matlab function to get text for display of object property

Asked by Jim Hokanson on 31 Mar 2012

I would like to change the way that the Matlab display function works on my object. I would like to reorganize the properties (which I know I could do if I redefined them in a different order), but additionally add a few more properties and descriptive lines. I would like to keep the functionality that Matlab offers by converting of all my properties to strings, truncating where necessary or simply summarizing by class and size in other cases. Does this functionality exist?

Here's an example:

                 auth_header: [1x1 struct]
            signature_method: 'HMAC-SHA1'

The structure is conveniently display as [1x1 struct].

Thus a function like the following would be great:

text = getPropText(obj,'auth_header');
text => '[1x1 struct]'

I could reproduce this functionality but it would be nice if I could just get access to the underlying function. Thoughts?

0 Comments

Jim Hokanson

Products

No products are associated with this question.

2 Answers

Answer by Jim Hokanson on 19 May 2012
Accepted answer

It seems like avoiding eval for so long I've forgotten that the input needs to be a string! The evalc approach isn't the greatest but it does let me focus on the organization and layout instead of how to format the property value string. Here's the code (NOTE: the regexp might not be the best ...):

%I call the object I am processing other_obj in this example, change to suit the name of your variable ...

    str     = evalc('builtin(''disp'',other_obj'')');   
    I_START = strfind(str,sprintf('Properties:\n'));
    I_END   = strfind(str,sprintf('<a href="matlab:methods'));
    props_and_text = regexp(str(I_START:I_END),'(?<prop_name>[^\s:]+):\s*(?<value>[^\n]+)','names');

0 Comments

Jim Hokanson
Answer by Ken Atwell on 1 Apr 2012

You can overload the display and/or disp methods for your object, which I believe will allow you to do what you want. See Displaying Objects in the Command Window within the object-oriented programming section of the MATLAB documentation.

2 Comments

Jim Hokanson on 2 Apr 2012

Ken, thanks for the post. Looking back I guess I was a bit unclear as to the problem. The problem is not how to overload the display, but rather whether or not the functions that are used to generate the display can also be used in my overloaded version.

Something like:
text = evalc(builtin('disp',myObj));
would even be sufficient, as I could parse this fairly easily, but it doesn't work :/

Ken Atwell on 3 Apr 2012

Gotcha, but I don't have a clear recommendation here. I don't know a direct why to capture the result of a disp instead of echoing to the Command Window.

You may be able to use the <http://www.mathworks.com/help/techdoc/ref/diary.html diary> command to capture the output to a file, and then parse that file.

Ken Atwell

Contact us