guidata(handles) does not save ALL handles in a Callback function in GUIDE

1 回表示 (過去 30 日間)
Farbos de Luzan
Farbos de Luzan 2018 年 2 月 21 日
コメント済み: Farbos de Luzan 2018 年 2 月 26 日
I have two callback functions defined as follow:
function hpos(hObject, pos, handles)
handles.A.L{handles.im}=value1;
guidata(hObject, handles);
function hpos2(hObject, pos, handles)
handles.A.R{handles.im}=value2;
guidata(hObject, handles);
They work fine until I try (I use the function "disp" here to get a visual feedback of what was saved):
disp(handles.A.L,handles.A.R)
Only the latest handles exists, as if it overwrote the previous one. So I get either
"Reference to non-existent field 'L'."
or
"Reference to non-existent field 'R'."
depending on what was the last updated field of the structure A.
Of course the goal is to use these value elsewhere in my code, so understanding how to save them correctly will help a lot! Thank you!

採用された回答

Jan
Jan 2018 年 2 月 22 日
編集済み: Jan 2018 年 2 月 22 日
From where are these callbacks called? Is handles the current value of the handles struct or does it contain the value of the struct as it was during the creation of the callback? The debugger will tell you this, simply set some breakpoints and check the contents of handles.
A solution is to get the current value of the struct at first:
function hpos(hObject, pos, handles)
handles = guidata(hObject);
handles.A.L{handles.im}=value1;
guidata(hObject, handles);
function hpos2(hObject, pos, handles)
handles = guidata(hObject);
handles.A.R{handles.im}=value2;
guidata(hObject, handles);
Or maybe another callback is responsible for resetting handles.A unexpectedly? Again the debugger will help you.
  1 件のコメント
Farbos de Luzan
Farbos de Luzan 2018 年 2 月 26 日
Thank you for the tip! The debugger just became my best friend. I made it work using your advice and many many attempts. I'm sure my code is not optimized and that the introduction of new functions may upset it, but I'm learning.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by