getappdata在MATLAB中返回空矩阵 [英] getappdata returning empty matrix in MATLAB

查看:236
本文介绍了getappdata在MATLAB中返回空矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码,其中使用setappdata,然后稍后使用getappdata调用数据,即使该数据不为空,它也会返回一个空的matirx.我的简化代码的一部分如下:

I have a piece of code in which I use setappdata and then later on I call the data using getappdata which returns an empty matirx even though it is not empty. A segment of my simplified code is below:

function edit1_Callback(hObject, eventdata, handles)

C=str2double(get(hObject,'String'))
setappdata(hObject,'H',C)

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)

N=getappdata(hObject,'H')

运行代码时,我在editbox中输入一个值,然后按pushbutton,我得到以下输出

When I run the code I enter a value into the editbox then push the pushbutton, I get the following output

C =

     5


N =

     []

我想要以下输出

C =

     5


N =

     5

我应该解释我正在使用getappdatasetappdata,因为我想在不同的GUI之间传递数据,并且在执行此操作时遇到了空矩阵问题.因此,这是我最终目标的一个非常简化的问题.我还阅读了很多不同的文章以及有关此问题和命令的信息,包括mathworks网站,但我对这个问题感到非常迷茫.

I should explain that I am using getappdata and setappdata as I want to pass data between different GUI's, and I am having the empty matrix problem when doing this. So this is a very simplified problem of my final goal. I have also read a lot of different articles and the information on this problem and commands, including the mathworks site but I am pretty lost with this problem.

推荐答案

首先,让我们解释发生了什么.

First, let's explain what's going on.

edit1_Callback中,您将setappdata应用于hObject.此时,hObject引用了edit1这个编辑框,并且您已将其应用程序数据值H设置为5.

Within edit1_Callback, you're applying setappdata to hObject. At this point hObject refers to edit1, the editbox, and you've set its application data value H to 5.

然后您在pushbutton1_Callback中调用getappdata.此时,hObject指的是pushbutton1,您将获得它的应用程序数据值H,该值从未设置过,因此得到[].

Then you're calling getappdata within pushbutton1_Callback. At this point hObject refers to pushbutton1, and you're getting its application data value H, which has never been set, so you get [].

先前的答案建议您改为在根对象0上使用setappdatagetappdata.这可以工作,但与使用全局变量BAD基本上相同.

A previous answer has suggested that you instead use setappdata and getappdata on the root object 0. This would work, but it's basically the same as using a global variable, which is BAD.

相反,我建议您最有可能只想确保您设置并获取正确数据上的应用程序数据.在edit1_Callback中,尝试:

Instead, I would suggest that you most likely want to just ensure that you're setting and getting the application data on the correct thing. Within edit1_Callback, try:

setappdata(handles.edit1,'H',C)

,然后在pushbutton1_Callback中,尝试:

N=getappdata(handles.edit1, 'H')

我认为这应该起作用(它假定编辑框实际上称为edit1,我认为这很可能是您的GUIDE生成的代码,但是如果您调用了其他名称,则可以更改它.)

I think that should work (it assumes that the editbox is actually called edit1, which I think is likely given your GUIDE-generated code, but change that if you've called it something else).

这篇关于getappdata在MATLAB中返回空矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆