clear all, close all, clc;

% If you want to create a struct with a field that contains a cell-array
% you have to be careful! This example will result in a 1x2 array of structs
% with:
%   my_wrong_struct(1).chars = 'A'
%   my_wrong_struct(2).chars = 'B'
my_crazy_struct = struct('chars', {'A', 'B'})

% If what you want to create is ONE struct with ONE field 'chars' that
% points to ONE cell array holding multiple values, you gotta enclose it in
% extra curly braces:
my_smooth_struct = struct('chars', {{'A', 'B'}})

% The Matlab documentation specifically says: "To create fields that contain 
% cell arrays, place the cell arrays within a value cell array."

% Another option is to declare your struct layout, before initializing it:
my_other_smooth_struct = struct('chars', [])
my_other_smooth_struct.chars = {'A', 'B'}

% The behavior gets really funky when you're not only creating a field that
% is supposed to hold a cell array, but also other fields which might be
% simple arrays. In the resulting _array of structs_ you will find those
% simple arrays duplicated over all indices of the array of structs:
my_crazy_struct2 = struct('chars', {'A', 'B'}, 'values', 1:10);
my_crazy_struct2(1)
my_crazy_struct2(2)

% While what you really wanted is probably:
my_smooth_struct2 = struct('chars', {{'A', 'B'}}, 'values', 1:10)

% This is really important when you try to dump return values from a
% function into a struct while creating it:

% Consider the function num2cell that converts numeric array to cell array. 
% Assume you want to convert the array [1 2 3] into a cell array {1 2 3} and
% add it to the field 'numbers' of a struct that you're creating on the
% fly. The intuitive attempt shown below, however, will result in an array
% of structs of length 3:
struct_on_the_fly = struct('numbers', num2cell([1 2 3]), 'values', 1:10)

% Here's how to do it right:
struct_on_the_fly2 = struct('numbers', {num2cell([1 2 3])}, 'values', 1:10)

% READ TROUGH THE OUTPUT IN THE COMMAND WINDOW TO UNDERSTAND WHAT'S
% HAPPENING!