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

 採用された回答

Guillaume
Guillaume 2015 年 2 月 19 日
編集済み: Guillaume 2015 年 2 月 19 日

1 投票

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 件のコメント

Kratos
Kratos 2015 年 2 月 19 日
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.
Guillaume
Guillaume 2015 年 2 月 19 日
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?
Kratos
Kratos 2015 年 2 月 19 日
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'
Guillaume
Guillaume 2015 年 2 月 19 日
編集済み: Guillaume 2015 年 2 月 19 日
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
Kratos
Kratos 2015 年 2 月 19 日
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.
Guillaume
Guillaume 2015 年 2 月 19 日
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
Kratos
Kratos 2015 年 2 月 19 日
It worked. Thank you for your time.

サインインしてコメントする。

その他の回答 (0 件)

質問済み:

2015 年 2 月 18 日

コメント済み:

2015 年 2 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by