turn char array input from function in variable name
이전 댓글 표시
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 ...
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!