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?

回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-10-15

0 个投票

6 个评论

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?
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.
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 2013-10-17
编辑:Gilmar 2013-10-17
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,...)

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Simulink 的更多信息

提问:

2013-10-15

Community Treasure Hunt

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

Start Hunting!

Translated by