Global variables in functions
Ältere Kommentare anzeigen
I have been trying to use global variables so I can make changes to the said variable in different functions but I can't seem to do it. It seems like the second function does not work. Please help.
function one=new1()
clc
clear global
global m
m=0;
disp('soup')
function hamburger=eat()
m=m+1
disp('sandwich')
Akzeptierte Antwort
Weitere Antworten (2)
dpb
am 11 Sep. 2017
>> help clear
...
| clear GLOBAL removes all global variables.
...
However, using global is not recommended as a general programming practice; you don't show enough context to see why there's any reason should be using one here so I'd suggest not doing so.
Raja Awais Liaqait
am 7 Okt. 2019
0 Stimmen
I want help in the following code.
global min_realvar ;
global max_realvar ;
Firstly, I want to get the value of these variables and secondly i want to write them in such away that I can give the values of these variables as an input.
1 Kommentar
Image Analyst
am 7 Okt. 2019
You already HAVE them since you put the global line in your workspace. No need to do anything more. Get them into another variable by referencing them.
someOtherVariable = min_realvar * 500 + 123424; % Whatever....
If you want, you can just pass them in as variables in the input argument list. Or you can write them to a disk file and pass the filename.
Kategorien
Mehr zu Scope Variables and Generate Names finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!