How to set and get value for a Data Store Memory.

16 Ansichten (letzte 30 Tage)
Patrick
Patrick am 5 Mär. 2024
Kommentiert: Fangjun Jiang am 14 Mär. 2024
Hello,
I would like to Read and Write in a Data Store Memory (DSM) with a Matlab function.
This matlab functions looks like :
  1. SetValue(GlobalVarName, NewValue)
  2. CurrentValue = GetValue(GlobalVarName)
The GlobalVarName shall be a string with the name of the DSM.
Currently, I have already created all my Simulink.Signal with the script, here an example:
  1. % Signal Vaux
  2. Vaux = Simulink.Signal;
  3. Vaux.Dimensions = 1;
  4. Vaux.Complexity = 'real';
  5. Vaux.DataType = 'double';
  6. Vaux.Description = '[Signal Vaux]; [Value = 0 to 20]; [Units = V]; ';
  7. Vaux.InitialValue = num2str(12);
In my Simulink Model, I use the block DSM Write in order to change the value Vaux.
I use a slider to change the value of a constant who are connected to my Vaux DSM.
But, now I would like to read a text file, with a set of value of Vaux, instead to use the slider.
I need to use the string name of the DSM to change its value because I have hundreds of values.
Can you help me?
Thanks.
  1 Kommentar
Patrick
Patrick am 5 Mär. 2024
Here you are an example of what I would like to perform
  1. Run CreateSimSig.m
  2. Run TestDSM_RW.slx
Then take a look in the Matlab function, it's what I would like to perform...
Here the code:
function updateAndReadDSMValues(file_path)
% Load data from the Excel file
data = xlsread(file_path);
% Get the list of DSMs in the Simulink model
dsm_list = find_system(gcs, 'SearchDepth', 1, 'BlockType', 'DataStoreMemory');
% Iterate through each row in the Excel file
for i = 1:size(data, 1)
% Get the variable name and its value from the Excel file
variable_name = data(i, 2);
variable_value = data(i, 3);
% Check if the variable name corresponds to a DSM in the model
if ismember(variable_name, dsm_list)
% Update the value of the DSM with the new value
set_param(variable_name, 'Value', num2str(variable_value));
disp(['The value of DSM "', variable_name, '" has been updated to ', num2str(variable_value)]);
% Introduce a delay of 50 ms
pause(0.05);
% Read the value of the variable in column 4
variable_to_read = data(i, 4);
read_value = evalin('base', variable_to_read);
% Write the read value to column 6
data(i, 6) = read_value;
else
disp(['No DSM corresponding to the variable name "', variable_name, '" found in the model.']);
end
end
% Save the updated data back to the Excel file
xlswrite(file_path, data);
end

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 13 Mär. 2024
  2 Kommentare
Patrick
Patrick am 14 Mär. 2024
Thanks for your answer but this link explain how to use DSM but there is no explanation about to set or get value.
I just realized that DSM or Simulink .Signal are like communication media but have no value.
It is therefore necessary to act on the parameters or on the displays.
Here's what I do:
function SetValue()
coder.extrinsic('set_param');
coder.extrinsic('getsimulinkBlockHandle');
path ='MyModel/C_Vaux'; % Where C_Vaux is the block parameter Constant connected to my DSM G_Vaux
h = getSimulinkBlockHandle(path,true);
set_param(h,'Value','13.5'); % Where Value is the value I want to set, 13.5 is the value I want to set
end
Same approach for GetValue
function GetValue()
coder.extrinsic('get_param');
coder.extrinsic('getsimulinkBlockHandle');
path ='MyModel/C_Vaux'; % Where C_Vaux is the block parameter Constant connected to my DSM G_Vaux
h = getSimulinkBlockHandle(path,true);
disp (get_param(h,'Value'); % Where Value is the value I want to get
end
So it works, I can set/get value of my Simulink model from a text file during the simulation in order to test my use case.
Fangjun Jiang
Fangjun Jiang am 14 Mär. 2024
Simulink parameter and signal are two different things. It was not clear what was your modeling needs. The question goes directly to how to access DSM. To get/set the value of the Constant block, you can use a variable name and set its value in the base workspace. It is absolutely not necessary to use a MATLAB function or get_param() and set_param(). The DSM is probably not needed either.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by