Why does "dir" function return an n by 1 struct?

17 views (last 30 days)
I am a novice at Matlab. I was reading about struct, and their size and that the size of a struct depends on the class of the value of the struct. If the struct values are not cells or if the cells are scalar, then the struct is 1 by 1. https://uk.mathworks.com/help/matlab/ref/struct.html
But when I look at the documentation for the 'dir' function: https://uk.mathworks.com/help/matlab/ref/dir.html?searchHighlight=dir&s_tid=doc_srchtitle
It says the class type in the struct are char, double and logical.
I looked at the class types documentation: https://uk.mathworks.com/help/matlab/matlab_prog/fundamental-matlab-classes.html
So these are their own class types, they are not cells, so why is the struct an n by 1 struct and not a scalar struct?

Accepted Answer

Matt J
Matt J on 8 Apr 2018
Edited: Matt J on 8 Apr 2018
If the struct values are not cells or if the cells are scalar, then the struct is 1 by 1.
The link you have cited applies specifically to inputs to the struct() command, when used to create MATLAB structure a array variables. The dir() command has its own rules.
The bottom line is, there is no connection between the dimensions of a struct array and the field data it contains. I could manually build a struct array of any dimensions regardless of whether its fields are cells, scalars, or whatever. Example:
>> s(2).cell={1,2,3,4}; s(1).scalar=5
s =
1×2 struct array with fields:
cell
scalar

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!