Accessing variables from workspace into user-created function block in Simulink?

A
Andersonwilliams · Apr 29, 2020 · 2.2K views
Question
As the title suggests, I defined some variable say, x in the workspace that has a number. Using MATLAB function, I am trying to import that variable (just once!) and as simulation progress, under certain condition, I want to increment that variable. However, since the function does not 'see' the variable x, I end up declaring that variable inside the MATLAB Function Block. However, this keeps initializing the variable and thus, I cannot increment it. I need help to resolve this problem!
Expert Answer
Profile picture of Neeta Dsouza
Neeta Dsouza PhD Expert
Answered Aug 24, 2026

You can declare your variable as persistent

function y=fcn(u)
persistent v
if isempty(v)
  v=your_constant
end
v=v+increment
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!