如何从我的自定义函数中将我自己的变量添加到句柄结构中?

发布于 2024-11-04 11:34:22 字数 1409 浏览 0 评论 0原文

我有一个问题要问 MATLAB 专家。

这里是我的代码(仅显示与问题相关的代码行):

mainProcess(hObject, handles)

handles.Checkpoint2 =1;
guidata(hObject, handles);

function testGUI1_OpeningFcn(hObject, eventdata, handles, varargin)

handles.output = hObject;
handles.Checkpoint1 = 1;
mainProcess(hObject, handles);
handles.Checkpoint3 = 1; //EDIT: Checkpoint2 is also not visible at this line
guidata(hObject, handles);
handles.Checkpoint4 = 1;


function saveAndContinueButton_Callback(hObject, eventdata, handles)

(breakpoint here) --> faceDatabase(handles.currentImageIteration).lookingTowardsCamera=handles.lookingAtCamera;

所以在上面的代码中,我在不同部分创建了这些“检查点”代码,并在单独单击“保存并继续”按钮时查看其中哪些是可见的...... Checkpoint1 是在调用我的自定义函数 mainProcess 之前创建的,Checkpoint2 是在 mainProcess 的代码中创建的,Checkpoint3 是在 mainProcess 执行完毕并且控制权返回到调用它的函数(testGUI1_OpeningFcn)之后创建的......而 Checkpoint4 是在 testGUI1_OpeningFcn 中创建,但是在 testGUI1_OpeningFcn 代码中更新句柄结构之后..

所以我的问题是,当按钮是单击,我看到此时可见的内容,检查点 1 和 3 对按钮回调代码可见,但检查点 2 和 4 不可见...我知道检查点 4 不可见,因为它是在句柄结构创建之后创建的在 testGUI1_OpeningFcn 的代码中更新...但是为什么 Checkpoint2 不可见,即使在 mainProcess 代码的末尾,我确实放了一行:

guidata(hObject, handles);

我的意思是 mainProcess 函数正在获取对 hObject 和 hObject 的引用句柄,所以它应该有写访问权限,对吧?

那么为什么 Checkpoint2 对按钮的回调代码不可见呢?

有什么线索吗?

编辑:我只是想看看即使在 mainProcess 的调用函数中,在控制权返回调用者之后,Checkpoint2 是否可见,甚至 Checkpoint2 也不可见(请参阅上面代码中的编辑)..

I have a question to ask the MATLAB gurus here ..

So here is my code (only showing lines of code which are relevant to the problem here):

mainProcess(hObject, handles)

handles.Checkpoint2 =1;
guidata(hObject, handles);

function testGUI1_OpeningFcn(hObject, eventdata, handles, varargin)

handles.output = hObject;
handles.Checkpoint1 = 1;
mainProcess(hObject, handles);
handles.Checkpoint3 = 1; //EDIT: Checkpoint2 is also not visible at this line
guidata(hObject, handles);
handles.Checkpoint4 = 1;


function saveAndContinueButton_Callback(hObject, eventdata, handles)

(breakpoint here) --> faceDatabase(handles.currentImageIteration).lookingTowardsCamera=handles.lookingAtCamera;

So in the above code, I'm making these 'checkpoints' at different parts of the code, and seeing which of them are visible when a save and continue button is clicked separately ...
Checkpoint1 is created BEFORE calling my custom function called mainProcess, Checkpoint2 is created within the code of mainProcess, and Checkpoint3 is created AFTER mainProcess is finished executing and the control is back with the function that called it, which is testGUI1_OpeningFcn ... And Checkpoint4 is created WITHIN testGUI1_OpeningFcn, but AFTER the handles structure is updated in the testGUI1_OpeningFcn code ..

So my question is this, when the button is clicked and I see what is visible at that point, Checkpoint 1 and 3 are visible to the button Callback code, but Checkpoints 2 and 4 are NOT visible ... I understand that Checkpoint4 is not visible because it was created AFTER the handles structure was updated in testGUI1_OpeningFcn's code ... But why is Checkpoint2 not visible, even when at the end of mainProcess's code, I did put a line:

guidata(hObject, handles);

I mean the mainProcess function is getting references to both hObject and handles, so it should have write access to it, right ?

So why isn't Checkpoint2 not visible to the button's Callback code .. ?

Any clues ?

EDIT: I just tried to see if Checkpoint2 is visible even within mainProcess's calling function, right after the control is returned the caller, and even there Checkpoint2 is not visible (see the EDIT in the code above) ..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苄①跕圉湢 2024-11-11 11:34:22

没错,您需要调用 guidata 来更新您拥有的handles 变量。然而,guidata 需要一个参数。我认为正确的命令是:

handles = guidata(hObject);  

您可能会发现此链接很有帮助:

That's correct, you need to call guidata to update the handles variable you have. However, guidata needs an argument. I think the right command would be:

handles = guidata(hObject);  

You may find this link helpful:

http://www.mathworks.com/matlabcentral/answers/10197-guidata-doesn-t-work-the-way-i-expected-it-to

姐不稀罕 2024-11-11 11:34:22

我相信您需要在调用 mainProcess() 之后添加以下内容。

handles = guidata();

一般来说,“handles”结构按值传递给 guidata() 函数。因此,mainProcess() 无法更改句柄结构——只需将现有结构附加到句柄即可。在进行进一步修改之前,您需要将其取回(使用handles = guidata()),更新它并使用guidata(h,handles)再次设置它。

如果这还不够清楚(或者只是不起作用:),请告诉我

编辑

您需要像这样更改代码:

function testGUI1_OpeningFcn(hObject, eventdata, handles, varargin)

handles.output = hObject;
handles.Checkpoint1 = 1;
mainProcess(hObject, handles);
handles = guidata();   // <--- new line

handles.Checkpoint3 = 1; //EDIT: now checkpoint2 will be visible here
guidata(hObject, handles);

handles.Checkpoint4 = 1;
guidata(hObject, handles); // Otherwise Checkpoint4 will not be bound to hObject

I believe you need to add the following just after calling mainProcess()

handles = guidata();

In general, the 'handles' struct is passed by value to the guidata() function. Therefore, mainProcess() cannot change the handles structure -- just attach the existing structure to the handle. Before making further modifications you need to get it back (using handles=guidata()), update it and set it again with guidata(h, handles).

Let me know if this is not clear enough (or just does not work :)

Edit

You need to change the code like this:

function testGUI1_OpeningFcn(hObject, eventdata, handles, varargin)

handles.output = hObject;
handles.Checkpoint1 = 1;
mainProcess(hObject, handles);
handles = guidata();   // <--- new line

handles.Checkpoint3 = 1; //EDIT: now checkpoint2 will be visible here
guidata(hObject, handles);

handles.Checkpoint4 = 1;
guidata(hObject, handles); // Otherwise Checkpoint4 will not be bound to hObject
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文