turn char array input from function in variable name
Ältere Kommentare anzeigen
Hi everyone,
I have a function which gets an input as char array, which simutaously is the name of a variable.
First I do a switch case to differntiate between little things, but then a longer script will follow in a for loop.
In this loop I want to change the global variable defined by the char array input. Of course I could copy the script just in every case of the switch but its long and I am curious if theres a better way. I thought of some kind of char array to variable name conversion, but my research showed it is not recommended.
global hello bye
function x = myfunction('varname')
switch varname
case 'hello'
hello = linspace(0, 10, 1000);
changevar = hello;
case 'bye'
bye = linspace(0, 20, 2000);
changevar = bye;
end
for i = 1:length(changevar)
hello = changevar(i)
bye = changevar(i)
% Here comes a longer skript where bye and hello are used in further functions which access them as a global variable
%If I call this function with 'hello' only the hello variable should be changed and other way round with 'bye'
% This is a simplification of the real problem and in my case the switch has many more cases, so just copying the for loop in every switch case is uncool
end
end
Any ideas are very welcomed. Thank you all, I am still learning to do programm with matlab ...
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Logical 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!