|
Prev: how to input video from a webcam to a simulink model in R14 SP3
Next: write from matlab to excel named range
From: Casper.Ptrsn on 6 May 2008 09:38 Perhaps i've missed something but is it possible (at all?) to write a function which returns this sort of "structure": dict = [1] [1x2 double] [2] [1x2 double] [3] [ 0] The example is taken from the Huffman Coding in the help section of MatLab.
From: Steven Lord on 6 May 2008 09:56 <Casper.Ptrsn(a)gmail.com> wrote in message news:6b135fa9-c1fa-417f-b75f-348aa3f3cd44(a)a23g2000hsc.googlegroups.com... > Perhaps i've missed something but is it possible (at all?) to write a > function which returns this sort of "structure": > > dict = > > [1] [1x2 double] > [2] [1x2 double] > [3] [ 0] > > The example is taken from the Huffman Coding in the help section of > MatLab. That's a cell array. Some function return cell arrays, particularly if they're returning lots of pieces of data that need to be "associated" with one another but don't have names (and as such aren't as well suited for a struct array.) You can create cell arrays like this: dict = {1, [1 2]; 2, [3 4]; 3, 0} or like this: % Create an empty cell array dict of the correct size dict = cell(3, 2); % Note how I'm using curly braces and parentheses in the code below % Assign the _double array_ 1 to be the _contents of the cell_ in the first row and first column of dict dict{1, 1} = 1; % Assign the _cell array_ containing [1 2] to _become the cell_ in the first row and second column of dict dict(1, 2) = {[1 2]} I'll leave filling in the remaining two rows as an exercise for you to become familiar with cell indexing and assignment. Study the lines of code I wrote, paying close attention to the use of curly braces {} and parentheses (). Of course, there's more information about cell arrays in the documentation: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f2-67323.html -- Steve Lord slord(a)mathworks.com
From: Casper.Ptrsn on 6 May 2008 10:02
> That's a cell array. Some function return cell arrays, particularly if > they're returning lots of pieces of data that need to be "associated" with > one another but don't have names (and as such aren't as well suited for a > struct array.) You can create cell arrays like this: > > dict = {1, [1 2]; 2, [3 4]; 3, 0} > > or like this: > > % Create an empty cell array dict of the correct size > dict = cell(3, 2); > % Note how I'm using curly braces and parentheses in the code below > > % Assign the _double array_ 1 to be the _contents of the cell_ in the first > row and first column of dict > dict{1, 1} = 1; > > % Assign the _cell array_ containing [1 2] to _become the cell_ in the first > row and second column of dict > dict(1, 2) = {[1 2]} > > I'll leave filling in the remaining two rows as an exercise for you to > become familiar with cell indexing and assignment. Study the lines of code > I wrote, paying close attention to the use of curly braces {} and > parentheses (). > > Of course, there's more information about cell arrays in the documentation: > > http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f2-... > > -- > Steve Lord > sl...(a)mathworks.com Thanks alot for you help. |