Global declaration not resolved to a Data Store Memory block registered via the Ports and Data Manager.

i was trying to make a predictive code algorithm however i recieve the same error every time .
code below.
function [Sa,Sb,Sc] = control(T_ref,sflux_ref,wm,i_meas)
% Variables defined in the parameters file
global Ts Rs Lr Lm Ls p tr kr r_sigma t_sigma lambda v states
persistent x_opt Fs %Fsp1 Tp1 g
if isempty(x_opt), x_opt = 1; end
if isempty(Fs), Fs = 0 + 0i*1; end
T_ref = T_ref(1) + 1j*T_ref(2);
sflux_ref = sflux_ref(1) + 1j*sflux(2);
i_meas = i_meas(1) + 1j*i_meas(2);
wm = wm(1) + 1j*wm(2);
% Stator flux estimate
Fs = Fs + Ts*(v(x_opt) - Rs*i_meas);
% Rotor flux estimate
Fr = Lr/Lm*Fs+i_meas*(Lm-Lr*Ls/Lm);
g_opt = 1e10;
for i = 1:8
% i-th voltage vector for current prediction
v_o1 = v(i);
% Stator flux prediction at instant k+1
Fsp1 = Fs + Ts*v_o1 - Rs*Ts*i_meas;
% Stator current prediction at instant k+1
Isp1 = (1+Ts/t_sigma)*i_meas+Ts/(t_sigma+Ts)*(1/r_sigma*((kr/tr-kr*1i*wm)*Fr+v_o1));
% Torque prediction at instant k+1
Tp1 = 3/2*p*imag(conj(Fsp1)*Isp1);
% Cost function
g = abs(T_ref - Tp1)+ lambda*abs(sflux_ref-abs(Fsp1));
if (g<g_opt)
g_opt = g;
x_opt = i;
end
end

18 comentarios

okay, and did you register each of the global variables as a datastore using the Port Manager?
i have tried couple of things one of which to use the data store memory block , and i have tried to register the variables in the port manager but it's not shown @Walter Roberso@Walter Roberson
i have read the full discussion over there but there are some point that i don't understand :-
1- where i shoud add the first part of the code starting from % Variables required by the control algorithm
% Sampling time of the predictive algorithm [s]
Ts_value = 4e-5; till
SS.InitialValue = mat2str(value);
end .. i quite belive this should be in the intial function call backs .
2- in the main function function [sa,sb,sc] = control(T_ref,sflux_ref,wm,i_meas,kr,Lm,p,r_sigma,t_sigma,tr) you have included multiple of inputs starting from Kr to tr which is not included in the refrence paramters from the induction motor paramters
That code all involved a specific model the user provided, in which that script was responsible for initializing variables and parameters, and then using sim() to run the model.
Adding additional parameters to the function was the cleanest way of importing Simulink parameters.
well fourtunately , am trying the same exact model that the user was trying to do actually the full model is being illustrated in the predictive control book . i mean have you tried to run the model and it worked ? am using matlab version 2016a so idon't know if there's a comptability problem.
so my question is does the first part of the code has to be implment in the call backs and i have to change all the given variables to somthing_value e.g. sf_nom has to be sf_nom_value to work with the code ??
and when i add the additional paramters to the model it has to get a signal from somewhere which i don't know ?
i have alos attached my model so you can see we are working almost on the same thing. . i even have tried the data store memory thing which you mentioned in your explantion then i find our that Simulink.Signal Object is better for multiple global data but still recieve no result .
I got the parameters and global variables working when I was helping the person. The overall code could not be run because of the unexpected 2x1 vector that I discussed in that Answer. (There is a bus that is being split into two entities instead of into three, so one ends up as a vector.)
alright so my question is i have to make a simulink signal to all global variables that includes : Ts Rs Lr Lm Ls p tr kr r_sigma t_sigma lambda v states ?? in the model explorer >> model workspace.
then i have to intialize the model variables till sim () in the call backs then rest of the codes in the matlab function , please correct me if am wrong ?
I have attached the editted models I created in that linked Question.
When you run this, you will quickly get an error. It will say
Error using run_MPC (line 67)
Errors occurred during parsing of MATLAB function 'MPC_DTC_IM/MATLAB Function'
This is because
Incorrect dimensions for matrix multiplication.
Check that the number of columns in the first matrix matches the number of rows in the second matrix.
To perform elementwise multiplication, use '.*'.
Function 'MATLAB Function' (#64.1097.1116), line 38, column 18: "(kr/tr-kr*1i*wm)*Fr"
The reason for this is what I explained in the previous question, about the bus with 3 elements being split into two locations, leading to a vector in an unexpected place. Which is outside of the domain of the question about how to set parameters and global variables.
unfourtantely i have matlab 2016a , i cannot run it . i understand the matrix problem but my question is do i have to put the first part of the code in the intial function in call backs or just put all of the code in the m file , excuse me am way to new to matlab and am trying to learn
This is specifically about using matlab to set up parameters and globals, and then use sim() to run the model. This code is not designed to be put into initfcn. It is potentially possible that some of it could be moved to initfcn callbacks, but it was not designed around that possibility.
it's designed to be put in m.file right
i have attached the final matlab file i have made following all the steps in this thread :
1- define a signal and data store memory for global variables.
2- defining all the code inside the m file and the intial function in call backs
3- checked that every blocks work just fine sattrting from the induction motor model to lc filter that i have made
4- i even get an outside space vector modulation controller to make sure that my model is working . however i still recieve errors regarding data type could you plese let me know how can i fix this . thank you so much
Hello, I have the same problem Did you solve the code? Could you please give me the corrected code?

Iniciar sesión para comentar.

Respuestas (1)

This error occurs because, in "MATLAB Function" block, global declarations are not mapped to the MATLAB global workspace. To use global data in your "MATLAB Function" block, you must:
  1. Declare a global variable in your MATLAB Function block.
  2. Register a Data Store Memory block that has the same name as the global variable as in the "MATLAB Function" block.
  3. In the Ports and Data Manager you may need to manually create the data entry, and change the name to match the Data Store Memory block, and set the scope to Data Store Memory.
  4. You will need to specify Dimensions, Signal type (real/complex), and Data type (e.g. double) for the variable in the corresponding Data Store Memory block.
For reference please run the below command in the command window to get the release specific documentation.
web(fullfile(docroot, 'simulink/ug/using-global-data-with-the-matlab-function-block.html'))
You can also refer to the below documentation link for more information.
Hope this helps!

Categorías

Más información sobre Modeling en Centro de ayuda y File Exchange.

Preguntada:

el 19 de Mayo de 2022

Respondida:

el 3 de Feb. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by