Does MATLAB Coder support 'persistent' variables?

10 次查看(过去 30 天)
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?

回答(2 个)

Walter Roberson
Walter Roberson 2015-7-28
Which statement is being identified as having the error? If it is the last statement then try initializing C_td near the beginning.
  1 个评论
JangHo Cho
JangHo Cho 2015-7-28
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.

请先登录,再进行评论。


Guillaume
Guillaume 2015-7-28
编辑:Guillaume 2015-7-28
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 个评论
JangHo Cho
JangHo Cho 2015-7-28
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 2015-7-28
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. :)

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by