Matlab coder - outputting variable sized cell arrays.

조회 수: 3(최근 30일)
Arwel
Arwel 2019년 1월 17일
댓글: Andy 2019년 1월 21일
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
Walter Roberson
Walter Roberson 2019년 1월 18일
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 .

댓글을 달려면 로그인하십시오.

답변(2개)

Andy
Andy 2019년 1월 17일
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
Walter Roberson
Walter Roberson 2019년 1월 17일
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" ?

댓글을 달려면 로그인하십시오.


Arwel
Arwel 2019년 1월 21일
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
Andy
Andy 2019년 1월 21일
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;

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by