I need help using the strrep for this code.

here are the three test cases. My question is how do I use strrep to replace the x of that function. The thing is it doesn't have to be x it can be anything but the basic layout is alway going to be functionName(varName) =
[val1] = ugaFunc('f(x) = 2*x^3', 5)
[val2] = ugaFunc('veloc(pos) = 2 / pos ^ 2 - 3', 0.5)
[val3] = ugaFunc('f(var_name) = 4 * (3 / var_name)^var_name-10*var_name',3.14)
function answer = hat(str, val)
% replace the input function with the value you want
a = strrep(str, ' ', 'val');
[b c] = strtok(a, '=');
[d e] = strtok(c, '0123456789 ');
% first number
[answer rest]= strtok(, '+-*/^');
answer = str2num(answer);
% a while loop to get a number and an operator each time
while (length(rest) ~= 0)
[operator rest]= strtok(rest, '0123456789 ');
[num rest] = strtok(rest, '+-*/^ ');
num = str2num(num);
% do the math
switch operator
case '+'
answer = answer + num;
case '-'
answer = answer - num;
case '*'
answer = answer .* num;
case '/'
answer = answer ./ num;
case '^'
answer = answer .^ num;
end
end
end

 Accepted Answer

Guillaume
Guillaume on 19 Feb 2015
Edited: Guillaume on 19 Feb 2015
You haven't said what you want to replace it with. The value specified in the second argument of your hat function?
In any case, to find the name of the variable, use regular expressions. For example, assuming a simple grammar where the function name is exclusively letters, the following would extract the variable name:
varname = regexp(str, '(?<=[A-Za-z]+\().*?(?=\))', 'match', 'once')
Note that there are many possible expression leading to the same result. The above looks for the shortest (lazy match) sequence of any characters (the .*?) immediately following (the (?<=....) one or more letters (the [A-Za-z]+) plus a bracket (the \(|) and immediately preceding (the |(?=...)) a closing bracket (the \)).
You can then replace the varname in the rest of the expression by the value with strrep or regexprep
newexpr = strrep(str, varname, num2str(val)); %is this what you want?

7 Comments

I didn't get what I wanted. Here is the question. That is, your function completely ignores order of operations and parentheses, and it evaluates all operators strictly left-to-right. You are guaranteed to only have +, -, *, /, and ^ as operators in the function. The left-hand side of the input function will always follow the form: functionName(varName) = There will always be one and only one operator between each number and/or variable in the input function. There will also not be any leading operators. The value that you evaluate the function at will always be greater than or equal to zero.
I didn't get what I wanted. Then what is it you want?
I have no idea what it is you're asking now. Have you just copied the text of an assignment, and hoping I'll do it for you?
If i run the code that I posted it works perfectly fine but I just don't how to replace the 'function(varName) = 2*varName^2' with the value I want. Say the value is 3. Then using the strrep the above string is going to be 'function(3) = 2*3^2'
Hum. This is exactly what my answer does (minus a forgotten argument in strrep which I've now added).
str = 'function(varName) = 2*varName^2';
val = 3;
varname = regexp(str, '(?<=[A-Za-z]+\().*?(?=\))', 'match', 'once');
newexpr = strrep(str, varname, num2str(val))
Output:
newexpr =
function(3) = 2*3^2
The problem is that you don't know what is going to be inside the parentheses. it could be f(varName) or just f(x) or anything. In this case how do you make sure to find the function at that value.
Oh, for Pete's sake, try the code. It makes no assumption on what's inside the brackets.
replacevar = @(expr, value) strrep(expr, regexp(expr, '(?<=[A-Za-z]+\().*?(?=\))', 'match', 'once'), num2str(value));
>>replacevar('f(x) = 2*x^3', 5)
ans =
f(3) = 2*3^3
>>replacevar('veloc(pos) = 2 / pos ^ 2 - 3', 0.5)
ans =
veloc(0.5) = 2 / 0.5 ^ 2 - 3
>>replacevar('f(var_name) = 4 * (3 / var_name)^var_name-10*var_name',3.14)
ans =
f(3.14) = 4 * (3 / 3.14)^3.14-10*3.14
It worked. Thank you for your time.

Sign in to comment.

More Answers (0)

Asked:

on 18 Feb 2015

Commented:

on 19 Feb 2015

Community Treasure Hunt

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

Start Hunting!