How can I override a value with an input?
Show older comments
I have a list of variables with values, but I want to be able to override one of those values with an input. To demonstrate:
function [output] = test(variable, value)
x = 2;
y = 3;
q = 10;
end
And let's say my input is (x, 5). Other than using a bunch of if statements, is there a way to make x = 5?
2 Comments
Azzi Abdelmalek
on 8 Aug 2014
This is not clear, variable and value are not used in your function
Andrew
on 8 Aug 2014
Accepted Answer
More Answers (1)
Geoff Hayes
on 8 Aug 2014
Could also just compare the input variable to a set of pre-defined "matches"
function [output] = test(variable, value)
x = 2;
y = 3;
q = 10;
if ischar(variable) && isvector(variable)
if strcmp(variable,'x')
x = value;
elseif strcmp(variable,'y')
y = value;
elseif strcmp(variable,'q')
q = value;
end
end
The above will guard against the case where variable is not a string, and is one of x, y, and q.
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!