Clear Filters
Clear Filters

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

1 view (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 12 Nov 2017
  3 Comments
Image Analyst
Image Analyst on 12 Nov 2017
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 on 12 Nov 2017
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

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 12 Nov 2017
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.

Categories

Find more on Function Creation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!