Is it possible to assign variables to a script?

I am running a script that uses the same notation for the equations, but one script is for the month of January and the other is for the month of May. Is there a way to keep the variables seperate if i was to run the scripts together to compare one against the other?

7 comentarios

... Make it into a function that you pass the data (or name of data file) into.
rammah nagi
rammah nagi el 2 de Ag. de 2019
Can you give me an example or link me to anywhere that shows me how please
doc function
rammah nagi
rammah nagi el 3 de Ag. de 2019
Editada: rammah nagi el 3 de Ag. de 2019
how would i plot the same variable from 2 different docs on the same graph? I have provided an example:
doc Jan
Pout = [1;2;3;4;5;4;3;2;1]
hour = 1:9
doc May
Pout = [5;4;3;2;1;2;3;4;5]
How do I plot these two on the same graph while the variables are both labelled Pout??
"doc function" was hint to read the documentation for the function keyword to learn the essence of writing functions...
You either write a function that calls the calculation with the necessary input to identify the case being operated on and return the result in different variables or as cell array or in this case it could be a 2D array since both are same length...that's your choice in how you wish to write it.
Or, you plot the first case, then use
hold on
and add the second...
Since we "know nuthink!" about what this function is, it's not possible to say much about actual specific code.
rammah nagi
rammah nagi el 3 de Ag. de 2019
Okay thank you, I had a look at the doc function and it doesnt seem possible.
Walter Roberson
Walter Roberson el 3 de Ag. de 2019
Editada: Walter Roberson el 4 de Ag. de 2019
function [hour, Pout] = MyFunction(month)
... Appropriate code that assigns to hour and Pout
Then
[h1, p1] = MyFunction('Jan');
[h2, p2] = MyFunction('May');
plot(h1, p1, h2, p2)

Iniciar sesión para comentar.

 Respuesta aceptada

Raunak Gupta
Raunak Gupta el 5 de Ag. de 2019
In my understanding, you want to assign different names to the returning variables from the scripts even if the notation is same in the equation that are mentioned in the script.
You may try to assign different names to the variables inside you script using the filename as a differentiator. For getting current filename you can use mfilename in script. Then the variable names can be dynamically assigned using eval adding the filename string to the variable. But the suggested method is to use function in both the scripts and then assign different variable names inside your current workspace for plotting those variables as mentioned in the comments. In case there are huge number of scripts to be compared based on the same variables then It is suggested to use methods mentioned in below link.
% This way you can change the name of variables based on script name
% For example creating a output from script Pout with a random vector
Pout = [1;2;3;4;5;4;3;2;1];
% Following will create a new variable based on the script's name
eval(['Pout_',mfilename,'=','Pout']);
clear Pout;
For further information about not using the dynamical naming use this:

Más respuestas (0)

Categorías

Productos

Versión

R2018a

Etiquetas

Preguntada:

el 2 de Ag. de 2019

Respondida:

el 5 de Ag. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by