Clear Filters
Clear Filters

Matlab coder - outputting variable sized cell arrays.

4 views (last 30 days)
Arwel
Arwel on 17 Jan 2019
Commented: Andy on 21 Jan 2019
Hi,
I am trying to use coder with a function that outputs a variable sized cell array. I've made a toy function that shows my problem (which I would very much like to copy and paste into here but this new editor doesn't let me), so I hope you can see the attached file (Mathworks - please fix asap).
This creates an output cell as follows
a = varCellCompileTest()
a =
1 x 3 cell array
{5 x 1 cell} {2 x 1 cell} {2 x 1 cell}
So 'result' is a 3 x 1 cell array of variable sized cell arrays, and each of those contain variable size doubles. So a cell array of variable sized cell arrays of variable sized double arrays. I can't figure out at all the coder.varsize that will allow me do do that.
Any ideas???
Cheers,
Arwel
  2 Comments
Ryan Livingston
Ryan Livingston on 18 Jan 2019
Your feedback about the copy and paste issue has been forwarded to the team that owns this editor.
Walter Roberson
Walter Roberson on 18 Jan 2019
With the new editor the input fields are not marked as text fields so your browser does not enable Paste in menus. However you can use your shortcut keys such as control V to paste .

Sign in to comment.

Answers (2)

Andy
Andy on 17 Jan 2019
Hi Arwel,
Codegen has two notions of a "variable sized" matrix. One is stack allocated and has some upper bound (for instance an array of up-to 20 elements), the other can be any size and is heap allocated.
If, for example, you wanted your cell array to be size 1x3 with elements that are cell arrays of size 1x:20 (where :20 means up to 20 elements) of matrices of size 1:20 then you could do the following:
result = {{[1] ; [1]} {[1] ; [1]} {[1] ; [1]}};
coder.varsize('result', [1, 3], [false, false]);
coder.varsize('result{:}', [20, 1]);
coder.varsize('result{:}{:}', [20, 1]);
If you want it to be heap allocated then ideally you would specify the upper bounds to be size "inf", however that doesn't seem to work we're internally investigating this. But in the meantime if you specify an extreme upper bound size coder will treat it as heap allocated. So for example the following:
BIG_VALUE = 655356;
result = {{[1] ; [1]} {[1] ; [1]} {[1] ; [1]}};
coder.varsize('result', [1, 3], [false, false]);
coder.varsize('result{:}', [BIG_VALUE, 1]);
coder.varsize('result{:}{:}', [BIG_VALUE, 1]);
-Andy
  1 Comment
Walter Roberson
Walter Roberson on 17 Jan 2019
Andy, I notice that you talk about 1x:20 but you code [20,1] ? I also notice that you do not add [true, false] to pin it down to true dimension of 1 instead of the 1 signalling "any number" ?

Sign in to comment.


Arwel
Arwel on 21 Jan 2019
Andy,
I'm afraid I still can't get this to work. I've tried to set it up as you suggest, initially just with one of my outputs to test.
I'm outputting froma cell array called 'reflectivity', which is created in sub functions just fine (I should point out everything works just fine so long as I dont try to output the result). So 'reflectivity' is......
reflectivity =
7 x 1 cell array
[51x2 double]
[51x2 double]
[51x2 double]
[51x2 double]
[51x2 double]
[51x2 double]
[51x2 double]
Then in my code I go.......
result = {{[1] ; [1]} {[1] ; [1]} {[1] ; [1]}};
coder.varsize('result', [1 3], [false false]};
coder.varsize('result{:}', [20 1], [1 0])
coder.varsize('result{:}{:}', [100 3], [1 1]);
for i = 1:7
result{1}{i} = reflectivity{i};
end
Everything compiles fine, but then when it tries to run it it gives an error in the loop.....
'Index exceeds array dimensions. Index value 3 exceeds valid range [1 - 2] of array result {1}'
I just don't get this at all..... I really need to get this working and am at my wits end here :(
Arwel
  1 Comment
Andy
Andy on 21 Jan 2019
Matlab coder does not allow resizing a cell array or matrix through indexed assignment (other than through end+1 in certain circumstances). You either need to reallocate the cell array using the cell() function or do a complete assignment to another cell array.
For your example, the easiest thing would be to change the loop to a complete assignment like this:
result{1} = reflectivity;

Sign in to comment.

Categories

Find more on MATLAB Coder 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!