How do I call a script inside a function?

So, I have three scrips two of them, initial_data1 and initial_data2, contain data and process it into arrays. Then I want the third scrip to run a function that uses the data from the files. I'm using matlab R2019a.
% initial_data2
dt = 0.01;
t = 0:dt:1000;
r = zeros(size(t)); %ect
The function I have works like this:
function [H,V,H_t,V_t] = Lunch_equations(data)
data;
% code
% also the output variabels are arrays if that is important.
end
And I'm trying to input the initial data files into the fuction so that the function has the local scope of one of these scrips at a time.
% The third scrip
clear
clc
[hight1,velocity1,hight_t1,velocity_t1] = Lunch_equations(initial_data1);
[hight2,velocity2,hight_t2,velocity_t2] = Lunch_equations(initial_data2);
%code
From here 'm getting an error: "Attempt to execute SCRIPT initial_data2 as a function" (or initial_data1).
Is there anyway to run the scrips inside the function as a local scope?
Thank you in advance.

 Risposta accettata

Ameer Hamza
Ameer Hamza il 1 Apr 2020
Modificato: Ameer Hamza il 1 Apr 2020
script name 'initial_data1' does not return a value. Running
initial_data1
will just run the code inside the script, and the variable will be created in the current scope. For example, if this script generate variables dt, t, and r the after running this line, you will be able to use those variables
initial_data1
% do something with dt, t, and r
Therefore, in your case, you can do somthing like this
initial_data1;
[hight1,velocity1,hight_t1,velocity_t1] = Lunch_equations(dt, t, r);
However, note that spawning the variables like this in a workspace can make the code very difficult to debug. An optimal code will just have one script for testing, the rest of the code should be organized in the form of functions.

4 Commenti

The problem with running it as a function and scrip like this would mean that I would need to 40 variables.
I could run it like this:
initial_data1;
% code from function with the outputs hight1, velocity1, hight_t1, velocity_t1
initial_data2;
% code from function with the outputs hight2, velocity2, hight_t2, velocity_t2
But that would be messy and would create a couple hundred lines of code to read.
I would love to have one single scrip with functions, however, I'm not sure how to add the individual sets of data then. Because, the initial data files are essential a set of initial data and creating the right arrays for them.
If you really want to keep the scripts and there are a lot of variables that you cannot easily pass, then I suggest you run the script file inside the function. You can pass the name of the script as input to the function. That will require a minimum change to your original code
[hight1,velocity1,hight_t1,velocity_t1] = Lunch_equations('initial_data1');
and change the function
function [H,V,H_t,V_t] = Lunch_equations(name)
run(data); % it will load all the variables inside the function workspace
% code
% also the output variabels are arrays if that is important.
end
However, in the future, you can consider refactoring your code by creating a struct with all the variables. Structs are easy to pass to functions and can hold all the data insider one variable name.
Thanks, this worked.
I'm going to look into the structures they probabaly will be helpfule later on.
Glad to be of help.

Accedi per commentare.

Più risposte (1)

David Hill
David Hill il 1 Apr 2020
Why not just use functions? I only use scripts when I am working in the workspace. Unless you are going to call a function more than once, you should just place the code in the original function (my opinion).

1 Commento

The code is going to be used a couple times and the initial data values have about 40 varibles in them that change between initial_data1 and initial_data2.
Like I mention in the above coment I could make it into three scripts and run it like this.
initial_data1;
% code from function with the outputs hight1, velocity1, hight_t1, velocity_t1
initial_data2;
% code from function with the outputs hight2, velocity2, hight_t2, velocity_t2
But, that would create couple hundred lines of code where a lot would be repeaded.

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by