Does MATLAB Coder support 'persistent' variables?

5 views (last 30 days)
Hello again.
I have another problem with MATLAB coder. Here is the code.
Function C_td = make_CA_matrix(C_array, A_array, k, N_h)
N_h : horizon length
global N_horizon
persistent count C_tilde;
if isempty(count)
count = 0;
count = count + 1;
if isempty(C_tilde)
C_tilde = zeros(4*N_horizon,3);
index_Cmat = k - N_h;
C_tilde(1:4,:) = C_array(:,:,index_Cmat);
N_state = size(A_array(:,:,1),2);
A = eye(N_state);
for i = 1 : N_h-1
index_Amat = k - N_h + i - 1;
A = A_array(:,:,index_Amat)*A;
index_Cmat = k - N_h + i;
C_tilde(i * 4 + 1: i * 4 + 4,:) = C_array(:,:,index_Cmat)*A;
C_td = C_tilde(1:N_h * 4, :);
When I tried to convert this MATLAB code to C code with MATLAB coder, This Error Message I met.
This assignment writes a 'double' value into a 'int32' type. Code generation does not support changing types through assignment. Check preceding assignments or input type specifications for type mismatches. (C_tilde)
Do you have any Idea to Fix this problem?

Answers (2)

Walter Roberson
Walter Roberson on 28 Jul 2015
Which statement is being identified as having the error? If it is the last statement then try initializing C_td near the beginning.
  1 Comment
JangHo Cho
JangHo Cho on 28 Jul 2015
Thank you Walter Roberson.
if isempty(C_tilde) C_tilde = zeros(4*N_horizon,3); <--Here is where the error is. end
I don't know why the error occurred. Anyway I'll read the article of the link you gave. Thank you.

Sign in to comment.

Guillaume on 28 Jul 2015
Edited: Guillaume on 28 Jul 2015
I suspect the error is on this line
C_tilde(1:4,:) = C_array(:,:,index_Cmat);
and that C_array is of class int32. Declare C_tilde of the same type:
C_tilde = zeros(4*N_horizon, 3, 'int32');
%or better, if supported by coder:
C_tilde = zeros(4*N_horizon, 3, 'like', C_array);
This has nothing to do with persistent.
JangHo Cho
JangHo Cho on 28 Jul 2015
Ah, I've tried one more thing and I think it worked.
When I tried code conversion with MATLAB Coder, I set the 'Enable variable-sizing' to 'No'. But this time I reset the value to default('Yes') and the function was generated successfully. The prototype of the function is like this.
void make_CA_matrix(const double C_array[120], const double A_array[90], int k, int N_h, double C_td_data[], int C_td_size[2])
Thank you for helping a confused dummy. :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!