Does MATLAB Coder support 'persistent' variables?

3 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;
else
count = count + 1;
end
if isempty(C_tilde)
C_tilde = zeros(4*N_horizon,3);
end
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;
end
C_td = C_tilde(1:N_h * 4, :);
end
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
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.
  2 Comments
JangHo Cho
JangHo Cho on 28 Jul 2015
Thank you, Guillaume.
I tried some things after reading your answer.
First thing I should have to do was to change the type of global variable N_horizon to int32 in MATLAB workspace (Say, N_horizon = int32(5);...) because I register N_horizon as int32 in MATLAB coder.
Next thing I should have to do was to modify the initialization routine of a persistent variable C_tilde.
if isempty(C_tilde) C_tilde = zeros(4*N_horizon,3); end
to if isempty(C_tilde) C_tilde = zeros(40,3); end
Then new problem occurred.
C_td = C_tilde(1:(N_h * 4), :); <<-
The error message was 'Could not determine the size of this expresstion.'
N_h is an input argument of the function make_CA_matrix. (function C_td = make_CA_matrix(C_array, A_array, k, N_h) So the size of C_td should depend on N_h.
Do you have any good idea?
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.

Categories

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