Clear Filters
Clear Filters

Why sim function clears workspace when is called from a script?

12 views (last 30 days)
I'm doing a simulation about solarcell/solarpanel with Simulink, it has 3 variablese irradiation, temperature and number of panels (N_parallel), it gets the parameters by a UI and after that values pass by a function call to the simulation, code goes fine but when "sim" function is called values are deleted and simulation does not work.
% Section of the UI that gets values and call funtion to Simulink
function bottom_Callback(hObject, eventdata, handles)
irradiacion = str2double(get(handles.iR,'String'));
temperatura = str2double(get(handles.tC,'String'));
N_parallel = str2double(get(handles.N_parallel,'String'));
opcion = 0;
if (get(handles.checkbox_PV,'Value')) == 1
opcion = 1;
elseif (get(handles.checkboxCV,'Value')) == 1
opcion = 2;
end
script_control_solarcell(irradiacion,temperatura,opcion,N_parallel); %funtion to Simulink
% call funtion to simulink
function [] = script_control_solarcell(irradiacion, temperatura,opcion,numero_paneles_ingresados)
vector_IR = irradiacion;
vector_TC = temperatura;
zise = length(vector_IR) * length(vector_TC);
mapa_color = parula( zise );
i = 0;
op_curvas = opcion;
N_parallel = numero_paneles_ingresados;
if op_curvas == 1
for temperatura = temperatura
for irradiacion = irradiacion
simout = sim("solarcell.slx"); % here workspace is cleared
i = i+1;
figure
plot(simout.Voltaje, simout.Potencia,'Marker','none', 'color', mapa_color(i,:), 'LineStyle','--','LineWidth',1,'DisplayName',['T=' num2str(temperatura) '^{\o}C, Ir=' num2str(irradiacion) 'W/m{^2}'])
.
.
.
.

Accepted Answer

Andés Corrales Castro
Andés Corrales Castro on 18 Feb 2022
I got a soluion for this issue,
set the variable in the workspace with command assignin(ws,var,val)
Simulink model look for variables in base workspace who is empty because valures are in GUI space.

More Answers (1)

Paul
Paul on 11 Feb 2022
Edited: Paul on 11 Feb 2022
How do irradiacion, temperatura, and N_parallel get pulled into the simulation? I thought the command
sim('solarcell.slx')
will look for parameters in the base workspace, not the function workspace. I think that may be the crux of the problem.
As for the function workspace being cleared (if that's really happening), open solarcell.slx then got to the Modeling Tab then Model Settings -> Model Properties -> Callbacks
Click on any callback function in the list marked with an asterisk and see if the code in the right hand pane has some sort of clear command that could clear the function workspace.
  3 Comments
Paul
Paul on 11 Feb 2022
Edited: Paul on 11 Feb 2022
My understanding is that, at least for block parameters, Simuink only looks in the base workspace when using the sim(model) command (for parameters that don't exist in the model workspace). There used used to be a sim() command name/value pair that was used to address this exact issue, I think the Name was SrcWorskpace and the value coulde be "Caller" or "Base" or something else. I don't think this is supported anymore and now the simIn has to be used instead.
I opened the example model 'vdp' and changed the Gain parameter of the block named Mu from the hardwired value of 1 to a variable muval.
Here's the simple function to execute the simulation:
function simout = tempfunc(in)
muval = in;
simout = sim('vdp');
end
Call the function from the command line:
>> tempfunc(1)
Error using tempfunc (line 3)
Invalid setting in 'vdp/Mu' for parameter 'Gain'.
Caused by:
Error using tempfunc (line 3)
Error evaluating parameter 'Gain' in 'vdp/Mu'
Error using tempfunc (line 3)
Unrecognized function or variable 'muval'.
Error using tempfunc (line 3)
Variable 'muval' has been deleted from base workspace.
Suggested Actions:
Load a file into base workspace. - Fix
Create a new variable. - Fix
Note that line about 'muval' deleted from the base workspace. I'm not sure why Simulink thinks the variable was deleted (I never even created it); it should probably say that 'muval' was not found in the base workspace. Regardless of the wording, it sure looks like it's looking for muval in the base workspace, not the function workspace, having not found it in the model workspace. And it exists in the function workspace, but sim still errored out.
I couldn't find anything on point in the doc, so I'm just speaking from experience, which is too bad because it seems like a basic issue for using sim() that should be clearly stated.
I'd be very interested in any examples where sim('model') grabs parameters from the function caller workspace without doing any magic in callback functions. I'm not even sure what that magic could be, but it might be possible.
Andés Corrales Castro
Andés Corrales Castro on 12 Feb 2022
hello! and tanks for comment this issue, and exactly there is no ducumentation about this problem with "sim" function, this error happend when I set a variable for the number of parallel cells, before that my Simulink model had just a constant of 2 and the variable of temperature, and it worked well but after the change happend the issue...

Sign in to comment.

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!