Is it possible to assign variables to a script?

조회 수: 11 (최근 30일)
rammah nagi
rammah nagi 2019년 8월 2일
답변: Raunak Gupta 2019년 8월 5일
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
rammah nagi
rammah nagi 2019년 8월 3일
Okay thank you, I had a look at the doc function and it doesnt seem possible.
Walter Roberson
Walter Roberson 2019년 8월 3일
편집: Walter Roberson 2019년 8월 4일
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)

댓글을 달려면 로그인하십시오.

채택된 답변

Raunak Gupta
Raunak Gupta 2019년 8월 5일
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:

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by