Script to function help

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

0 votes

6 Comments

Gilmar
Gilmar on 15 Oct 2013
Isn't there an easier way? instead of making another simulink module? I thought I could just make the whole function a global scope? because it seems like to me from what I read its that both scripts and functions use their own "workspace" that is locally stored and not globally. So I don't know if I just change the permissions if that could work.
Can you explain how your functions are used in your Simulink model?
Gilmar
Gilmar on 17 Oct 2013
the simulink model takes some values that were stored in workspace through the script. then it does the simulation and saves those values. And returns to the script where it continues to run the the rest of the script using those saved simulink values.
Also is there a way where I can just tell simulink to look at the "workspace" that the function is using?
Sorry I'm being a bit vague, but I can't really tell you the code being implemented... for reasons.
Azzi Abdelmalek
Azzi Abdelmalek on 17 Oct 2013
Edited: Azzi Abdelmalek on 17 Oct 2013
If I've understood, You run your script, after you run your simulink model which will save some result via to workspace block. At the end of your simulation you will run another script. Until now I don't know what is your problem?
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.
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.

Categories

Find more on Simulink in Help Center and File Exchange

Asked:

on 15 Oct 2013

Commented:

on 17 Oct 2013

Community Treasure Hunt

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

Start Hunting!