## Loren on the Art of MATLABTurn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

# A Best Friend, struct2table

Sometimes it's nice to have a best friend. Sometimes I have more than one, context-dependent. I want to share my best friend today for dealing with struct arrays.

Structure arrays are a useful way to collect similar information for a collection of items, even if what gets collected is not always the same size. However, struct arrays can also be cumbersome to work with. Thanks to the table datatype, I can now avoid some of the intricacies of indexing into struct arrays by simply performing a conversion to table first, and then using good, old-fashioned, stands the test of time, MATLAB indexing.

### Contents

#### Some examples

Some examples for working with struct arrays after converting to a table include:

1. output of dir - (I can then remove dirs, etc. very easily)
2. jsondecode
3. regionprops from Image Processing Toolbox - (now includes output to table option)

#### Let's try it

I'll now get the listing for the blog post directory for one of my guest authors, Alan Weiss.

dirstruct = dir('AlanW')

dirstruct =
5×1 struct array with fields:
name
folder
date
bytes
isdir
datenum


And here's a picture from the Windows Explorer

You can readily see that I really have only 3 items, and not the 5 suggested in dirstruct. I can, of course, now look at the names. I extract them via a comma-separated list.

dirstruct.name

ans =
'.'
ans =
'..'
ans =
'Clumping'
ans =
'FinSymbolic'
ans =
'Sudoku'


and now you can see that I really don't care about the first two. But I'd have to delete these from the name field, and the folder field, and ... So I definitely can do so, but what a hassle.

Instead let me convert to a table.

dirtable = struct2table(dirstruct)

dirtable =
5×6 table
name                   folder                        date             bytes    isdir     datenum
_____________    ___________________________    ______________________    _____    _____    __________
'.'              'C:\Work\ArtofMATLAB\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05
'..'             'C:\Work\ArtofMATLAB\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05
'Clumping'       'C:\Work\ArtofMATLAB\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05
'FinSymbolic'    'C:\Work\ArtofMATLAB\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05
'Sudoku'         'C:\Work\ArtofMATLAB\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05


And now I can eliminate the first 2 rows in the usual way in MATLAB:

dirtable(1:2,:) = []

dirtable =
3×6 table
name                   folder                        date             bytes    isdir     datenum
_____________    ___________________________    ______________________    _____    _____    __________
'Clumping'       'C:\Work\ArtofMATLAB\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05
'FinSymbolic'    'C:\Work\ArtofMATLAB\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05
'Sudoku'         'C:\Work\ArtofMATLAB\AlanW'    '22-Sep-2017 02:50:46'    0        true     7.3696e+05


#### Have you replaced your use of struct arrays with tables?

I'm wondering if you've used struct arrays before and are now replacing them with tables. And if you have an application where that does not make sense, I'd love to hear about that as well. Let me know here.

Published with MATLAB® R2017b