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
0 件のコメント
採用された回答
Image Analyst
2017 年 11 月 12 日
3 件のコメント
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.
その他の回答 (1 件)
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!