Script to function help

8 次查看(过去 30 天)
Gilmar
Gilmar 2013-10-15
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
  6 个评论
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.
Azzi Abdelmalek
Azzi Abdelmalek 2013-10-17
编辑:Azzi Abdelmalek 2013-10-17
Just use the variables needed by simulink model v1,v2,...as output argument in your function
[var1,var2,...]=you_function(x1,...)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Sources 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by