Global declaration not resolved to a Data Store Memory block registered via the Ports and Data Manager.
Mostrar comentarios más antiguos
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
Walter Roberson
el 19 de Mayo de 2022
okay, and did you register each of the global variables as a datastore using the Port Manager?
amr makhlouf
el 22 de Mayo de 2022
amr makhlouf
el 22 de Mayo de 2022
Walter Roberson
el 22 de Mayo de 2022
You might be interested in my discussion at https://www.mathworks.com/matlabcentral/answers/1653710-global-variable-in-simulink#comment_2009495
amr makhlouf
el 24 de Mayo de 2022
Walter Roberson
el 24 de Mayo de 2022
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.
amr makhlouf
el 24 de Mayo de 2022
Walter Roberson
el 24 de Mayo de 2022
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.)
amr makhlouf
el 24 de Mayo de 2022
Walter Roberson
el 26 de Mayo de 2022
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.
amr makhlouf
el 27 de Mayo de 2022
Walter Roberson
el 27 de Mayo de 2022
m file.
Walter Roberson
el 27 de Mayo de 2022
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.
amr makhlouf
el 28 de Mayo de 2022
Walter Roberson
el 28 de Mayo de 2022
yes
amr makhlouf
el 28 de Mayo de 2022
akram amri
el 8 de Feb. de 2024
Hello, I have the same problem Did you solve the code? Could you please give me the corrected code?
Franklin Sánchez
el 15 de En. de 2025
How did you solve de code help please
Respuestas (1)
Gayathri
el 3 de Feb. de 2025
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:
- Declare a global variable in your MATLAB Function block.
- Register a Data Store Memory block that has the same name as the global variable as in the "MATLAB Function" block.
- 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.
- 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.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
