3

I'm trying to generate C-Code from a MATLAB function. I have to work with cell arrays (I cannot change this) and there are occurring two problems:

1: I want to truncate a cell array, i.e.

arr = cell(1,n); 
% ...
arr = arr(1:m);   % with m<n

MATLAB Coder forbids the (...) indexing, so I tried the following

tmp = arr;
arr = cell(1,m);
for i = 1:m
    arr{i} = tmp{i};
end

But this will throw the error, that 'Matlab is unable to determine that every element of tmp{:} is assigned before this line'. The same happens if I assign every element of arr to tmp in a loop.

  1. The same problem occurs when I try to concatenate two cell arrays.

Do you know how to fix this, so that MATLAB Coder won't throw any errors.

4
  • Interesting that it won't acknowledge tmp = arr; as a copy... maybe the corer takes a pointer sometimes and its getting confused here. Can you initialize temp before the copy with tmp=cell(1,n);? Maybe even making a loop for tmp = arr;, instead of a single line Commented Jun 21, 2019 at 12:42
  • I tried to copy arr to tmp by a loop, but that gave the same error message. Commented Jun 21, 2019 at 12:46
  • Including the initialization I suggested? Commented Jun 21, 2019 at 12:47
  • yes, exactly... Commented Jun 21, 2019 at 13:06

2 Answers 2

2

Depending on what version of MATLAB Coder you're using you can get this error. In order to avoid the complete assignment detection problem you can type:

arr = coder.nullcopy(cell(1,m));

But you need to promise you're writing to every cell element after that assignment (which you are doing, because

for i = 1:m
    arr{i} = tmp{i};
end
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this with embedded curly braces.

arr_trunc = {arr{1:m}}

I hope this helps. I tested this with Coder and didn't get an error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.