File Exchange Pick of the Week

Our best user submissions

Writing a struct to text

Brett‘s Pick this week is struct2str, by Marco Cococcioni.

Contents

Strings from structs

Back in May, Sean wrote about different File Exchange submissions that facilitate writing cell arrays to text. That was a useful post–and one that triggered an interesting follow-up discussion between Sean and me: namely, how would one write a struct to string? I write a lot of apps, and frequently want to put the contents of a struct into a listbox, for example. That can be challenging and frustrating.

When I suggested that Sean follow up with a post on writing structs to text, he quickly agreed that that would be interesting. But I think he initially missed my point: he suggested that I could easily write the fieldnames of a structure to a listbox using, for instance,

lb = uicontrol('units','normalized',...'position',[0.1 0.1 0.8 0.8],...
	'Style','listbox');
d = dir('.\*');
lb.String = fieldnames(d);

“True,” I replied. “But I want the contents of the struct, not just the fieldnames.” His reply: “How about”

names = {d(:).name};
lb.String = names;

Okay, that gets me the values of a single fieldname (in this example, ‘name’). However, assume that I get the file information for an image, and want to drop it all in a textbox or listbox:

structInfo = imfinfo('street2.jpg')
structInfo = 
           Filename: 'C:\Program Files\MATLAB\R2015a\toolbox\matlab\demos\...'
        FileModDate: '22-Mar-2004 19:54:40'
           FileSize: 39920
             Format: 'jpg'
      FormatVersion: ''
              Width: 640
             Height: 480
           BitDepth: 24
          ColorType: 'truecolor'
    FormatSignature: ''
    NumberOfSamples: 3
       CodingMethod: 'Huffman'
      CodingProcess: 'Sequential'
            Comment: {}

How could I easily put all of that information into a listbox?

struct2str

That’s where Marco’s file comes in:

lb.String = struct2str(structInfo);

I had initially done this with a particularly ugly for loop, but struct2str makes it very easy. (Marco used some loops, too, but his code alleviates for me the pain of writing that loop. Thanks, Marco!)

And a challenge!

Note that there are a few empty fields in the imfinfo output struct in the usage above. Kudos and swag to the first person who replies with an elegant approach to automatically removing from the struct the fields with empty values. Use any approach you’d like, including other File Exchange submissions. What I’d like to see is this:

structInfo = rmfield(structInfo,{'FormatVersion','FormatSignature','Comment'})
structInfo = 
           Filename: 'C:\Program Files\MATLAB\R2015a\toolbox\matlab\demos\...'
        FileModDate: '22-Mar-2004 19:54:40'
           FileSize: 39920
             Format: 'jpg'
              Width: 640
             Height: 480
           BitDepth: 24
          ColorType: 'truecolor'
    NumberOfSamples: 3
       CodingMethod: 'Huffman'
      CodingProcess: 'Sequential'

…but without the manual specification of the empty fields. Let me know how you would approach this, and I will select (and reward) the one I like best. It’s good to be the blogger. ;)

Comments?

As always, comments are welcome! Let me know what you think (or respond to my challenge) here, or leave feedback for Marco here.




Published with MATLAB® R2015a

|
  • print

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.