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

조회 수: 8 (최근 30일)
Kevin Lebras
Kevin Lebras 2023년 4월 6일
답변: Sachin 2023년 4월 10일
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에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by