variables are not saved in workspace after function.

function = demo()
figure('WindowButtonDownFcn',@callBack)
function callBack(hObject,~)
mousePos=get(hObject,'CurrentPoint');
x = mousePos(1)
y = mousePos(2)
So when I am lunching the function x and y are ploting in 'Command Window', but they aren,t saved in workspace.
pleas Help!!!

回答 (4 件)

dpb
dpb 2019 年 6 月 3 日

1 投票

It can't--you don't return anything. Read about functions at
Stephen23
Stephen23 2019 年 6 月 4 日

0 投票

You need to explicitly pass those variables between workspaces, exactly as the MATLAB documentation describes:
Avoid using global and assignin. If you are sensibly writing your own GUI code then I recommend that you use nested functions for passing data between callbacks, because these are simple and intuitive:
function [x,y] = demo()
x = [];
y = [];
fgh = figure('WindowButtonDownFcn',@callBack)
waitfor(fgh)
function callBack(hObject,~)
mousePos=get(hObject,'CurrentPoint');
x = mousePos(1)
y = mousePos(2)
end
end
luka gvichiani
luka gvichiani 2019 年 6 月 6 日

0 投票

thank u but when I run this function only x is saveing as 'ans'

5 件のコメント

Walter Roberson
Walter Roberson 2019 年 6 月 6 日
[x, y] = demo();
luka gvichiani
luka gvichiani 2019 年 6 月 6 日
yes i know but when I run that I get only x as 'ans'
Walter Roberson
Walter Roberson 2019 年 6 月 6 日
ans is only ever one variable. ans is only updated when you do a calculation but do not assign the results to a location. The only way you could get ans out of that is if you did not follow my instructions -- if you ran the code as just
demo
or if you clicked the green run button. You need to go down to the command window and type in
[x, y] = demo();
luka gvichiani
luka gvichiani 2019 年 6 月 6 日
Thank you ,that realy helped.
<3<3 :)
Stephen23
Stephen23 2019 年 6 月 7 日
@luka gvichiani: please remember to accept my answer!

サインインしてコメントする。

luka gvichiani
luka gvichiani 2019 年 6 月 6 日

0 投票

I want to save bouth of them:)

カテゴリ

ヘルプ センター および File ExchangeDevelop Apps Using App Designer についてさらに検索

質問済み:

2019 年 6 月 3 日

コメント済み:

2019 年 6 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by