Use publish() with input variables for function with input object

9 次查看(过去 30 天)
Hello,
I want to use the publish() call with a function that has an input object. The only documentation I can find allows you to have a numerical input but not a variable input. This example is given in the documentation
publish('fact.m','codeToEvaluate','fact(5);')
web('html/fact.html')
I want to run the same function but replace "5" with an input variable "x". Is there a way to accomplish this?
x=5
publish('fact.m','codeToEvaluate','fact(x);')
web('html/fact.html')
If I run the above example I get the following warning
"Error using evalin
Argument must be a text scalar."

回答(1 个)

Sachin
Sachin 2023-4-10
Hi
I understand that you want to use publish() function with a variable name so this workaround might be of good help to you.
You use string concatenation to create the string with the fact() function that can be passed as a third argument to the publish function.
x = 5;
codeToEvaluate = ['fact(', num2str(x), ');'];
publish('fact.m', 'codeToEvaluate', codeToEvaluate);
web('html/fact.html');
In the above code, we are creating the string “codeToEvaluate” that includes the value of x with function fact().
Thanks
Sachin

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by