Matlab: Understanding a piece of code

Zanam

I have a matlab code which is for printing a cell array to excel. The size of matrix is 50x13.

The row 1 is the column names.

Column 1 is dates and rest columns are numbers.

The dateformat being defined in the code is:

dFormat          =   struct;
dFormat.Style    =   struct( 'NumberFormat', '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)' );
dFormat.Font     =   struct( 'Size', 8 );

Can someone please explain me what the dFormat.Style code means ?

Thanks

horchler

The first line creates an empty struct (struct with no fields) called dFormat. A structure can contain pretty much anything in one of its fields, including another structure. The second line adds a field called 'Style' to the dFormat struct and sets it equal to another struct with a field called 'NumberFormat'. The 'NumberFormat' field is set equal to that long string of characters. You now have a structure of structures. The third line is similar to the second.

Note that the first line isn't really necessary unless dFormat already exists and it needs to be "zeroed out" as dFormat.Style with create it implicitly. However, using the struct function can make code more readable in some cases as objects use a similar notation for access methods and properties. In other words, all of your code could be replaced with:

dFormat.Style.NumberFormat = '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)';
dFormat.Font.Size = 8;

See this video from the MathWorks for more details and this list of helpful structure functions and examples.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related