Clear Filters
Clear Filters

Script to function help

8 views (last 30 days)
Gilmar
Gilmar on 15 Oct 2013
Commented: Azzi Abdelmalek on 17 Oct 2013
So I have a script that uses simulink, and it calls upon stuff saved in workspace, but I now need to change the script into a function that gives me 4 outputs and accepts 20 inputs. I have something like this set up:
function[a,b,c,d] = myfunc(x1,x2,x3,...x20)
but the problem I'm getting is that the simulink is not getting any values being saved. I think it has to do with functions not saving to the normal workspace. What I'm trying to find out is how I can just make my whole function save its variables into workspace?

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 15 Oct 2013
  6 Comments
Gilmar
Gilmar on 17 Oct 2013
Edited: Gilmar on 17 Oct 2013
No, I have this one pretty long script. When I run the script the first 1/4th of the script sets the initial values of some variables that are supposed to be user-inputted, but is hard-coded since it's a script. Currently the script takes those initial variables saves them in workspace like most scripts do, and the script continues to run, while the script is running there's a line in the script that calls a specific simulink module already created, and that simulink module gets the values from the script, then after the simulink module is done running it returns that information to the same script, where the script continues to do some equations using the information obtained from the simulink module. I run into a problem when I try to change the hard-coded information into a function with user inputs, because I need it to accept user information and not have it always be hard-coded in the script.
So when I try running it as a function the line that calls upon my simulink module is not getting the values like it used to. and I think the reason why is because the simulink module just looked into workspace for those values, but since it is a function that information does not get saved to workspace anymore.
So I don't know if I can tell the simulink module to look for that information somewhere else, or if I can just set all my variables as global so simulink can get it?
Also sorry if this still doesn't make that much sense.
Azzi Abdelmalek
Azzi Abdelmalek on 17 Oct 2013
Edited: Azzi Abdelmalek on 17 Oct 2013
Just use the variables needed by simulink model v1,v2,...as output argument in your function
[var1,var2,...]=you_function(x1,...)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!