필터 지우기
필터 지우기

Is it okay the way you saved one function in a guide and then I use it in another?

조회 수: 1 (최근 30일)
I asked for it in a guide
p=inline(get(handles.edit1,'string'))
setappdata (0, 'string', p)
and to use it in another guide towards this
p = getappdata (0, 'string')
f=p

채택된 답변

Image Analyst
Image Analyst 2017년 11월 12일
  댓글 수: 3
Image Analyst
Image Analyst 2017년 11월 12일
It shares variables, NOT pure numbers contrary to what you said. In other words, you can share "x" but you can't share the number 73 by itself - it has to be in a variable and then you can share it.
If you square x you need to put the result into some variable. Then just share that.
Erwin Avendaño
Erwin Avendaño 2017년 11월 12일
I had an error, it is that I wanted to pass an "inline" function my mistake if it worked for me thank you very much

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 11월 12일
Yes. But be aware that
setappdata (0, 'string', p)
is confusing. The result of inline() is not a string, but the code works anyhow because setappdata() accepts arbitrary tag names. There no attribute named string for setappdata(): as far as setappdata is concerned, the code
setappdata(0, 'BensonArizonaWithTheWarmWindThroughYourHair', p)
would do the same job... but with less confusion to the readers.
"It only serves me for numbers I want to pass but variables, as well as x ^ 2 but I do not want to use syms or char"
inline() does not share variables.
>> A = 3
A =
3
>> p = inline('A*x', 'x')
p =
Inline function:
p(x) = A*x
>> p(5)
Error using inlineeval (line 14)
Error in inline expression ==> A*x
Undefined function or variable 'A'.
Error in inline/subsref (line 23)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Compare this to anonymous functions:
>> q = @(x) A*x
q =
function_handle with value:
@(x)A*x
>> q(5)
ans =
15
At the time you construct an anonymous function, MATLAB searches through the expression you give, and for each variable named that is not listed in the @() section, it takes a copy of the variable as it exists then and attaches the copy to the anonymous function.
If you wanted to construct some kind of function that you could pass around that would reference the current values of variables that were constructed in a different workspace, then you would need to either use handle variables or else pass around a function handle to a nested function inside the same workspace as the variables to be shared.

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by