0
votes

I have a MATLAB function block (named Q_learning in the visual below) in Simulink. The code needs to update an existing matrix (denoted by 'Q') which was initially declared in a script (initialpara.m file) as a 7 by 10^7 matrix of zeros.

But it appears that it cannot access the initial declaration of the matrix. The error message is as follows:

Undefined function or variable 'Q'. The first assignment to a local variable determines its class.

Function 'Control Centre/MATLAB Function' (#174.774.775), line 32, column 21: "Q" Launch diagnostic report.

initialpara.m file

%%% Q Learning Parameters %%%

Q = zeros(7,100*100*1000);

gamma = 0.8;

previous_state = 0;
previous_reward = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Q_learning.m file

function [KP, KI, KD, state, reward] = Qlearning(e_now, previous_state, previous_reward)
%#codegen
    if (e_now >= -1 && e_now <= 1)
        state = 1;
        reward = 7;

    elseif (e_now >= -5 && e_now <= 5)
        state = 2;
        reward = 6;

    elseif (e_now >= -10 && e_now <= 10)
        state = 3;
        reward = 5;

    elseif (e_now >= -15 && e_now <= 15)
        state = 4;
        reward = 4;

    elseif (e_now >= -20 && e_now <= 20)
        state = 5;
        reward = 3;

    elseif (e_now >= -25 && e_now <= 25)
        state = 6;
        reward = 2;

    elseif (e_now >= -30 && e_now <= 30)
        state = 7;
        reward = 1;
    end#

    subMatrix = Q(state, :);
    [maxQ_value, max_column] = max(subMatrix);

    Q(state, previous_state) = previous_reward + 0.9 * maxQ_value;

    [KP, KI, KD] = action_decode(max_column);
end

I have tried to declare the variable 'Q' in the model workspace (although it was already declared in the base workspace) but it didn't solve the problem.

Can anyone help me solve this issue? Thanks!

enter image description here

2

2 Answers

0
votes

Declare Q global. See help global in MATLAB.

0
votes

In this case, you need to 'declare' Q. You could do it going to Edit Data option and then Add Data. Follow the configuration options. Another way is to define the signal in Simulink and give it as a parameter to the Matlab function. By the way, it's a good practice to keep the read signals on the left side and out - on right. This improves a lot the readability.