在 GUI MATLAB 中调用局部变量时出错

发布于 2024-09-03 02:55:06 字数 278 浏览 7 评论 0原文

我收到此错误:

Error in ==> APP>pushbutton2_Callback at 109
img=imread(FileName)

当我尝试在 pushbutton2_Callback 中使用 FileName 时,我收到提到的错误

FileName is variable in Pushbutton1_Callback

I'm getting this error:

Error in ==> APP>pushbutton2_Callback at 109
img=imread(FileName)

When I try to use FileName in pushbutton2_Callback I'm getting the error mentioned

FileName is variable in pushbutton1_Callback.

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

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

发布评论

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

评论(2

旧梦荧光笔 2024-09-10 02:55:06

您需要将变量 FileName 从一个回调传递到另一个回调。为此,您可以将该变量分配给 pushbutton1'UserData' 字段。 pushbutton1_Callback 下的代码应类似于:

FileName=uigetfile();
set(handles.pushbutton1,'UserData',FileName);

接下​​来,您需要读入 pushbutton2_Callback 下的变量:

FileName=get(handles.pushbutton1,'UserData');
img=imread(FileName);

如果您想检查结果,可以随时保留分号超出了线路的末端。

You need to pass the variable FileName from one callback to the other. To do this, you can assign the variable to the 'UserData' field of pushbutton1. Your code under pushbutton1_Callback should look something like:

FileName=uigetfile();
set(handles.pushbutton1,'UserData',FileName);

Next, you need to read in the variable under your pushbutton2_Callback:

FileName=get(handles.pushbutton1,'UserData');
img=imread(FileName);

If you want to check your results, you can always leave the semicolons off the end of the lines.

东北女汉子 2024-09-10 02:55:06

有一种通用方法可以使用 gui 存储数据,以便在回调之间使用。您可以将任意字段添加到handles对象,这样您就可以放入pushbutton1回调中。

handles.filename = FileName;
guidata(hObject,handles); 

第二行是样板代码,您需要将其放在任何更改handles结构中的值的回调的末尾。

现在您的所有回调都可以访问该文件名。在您的具体情况下,在回调 2 中,您当然

img = imread(handles.filename);

可以稍后在另一个函数中使用该图像,因此您也可以将其存储在句柄中

handles.img = img;
guidata(hObject, handles);

There's a general method to store data with your gui for use between callbacks. You can add arbitrary fields to the handles object, so you can put in your pushbutton1 callback

handles.filename = FileName;
guidata(hObject,handles); 

The second line is boilerplate code that you need to put at the end of any callback that changes values in the handles structure.

Now all of your callbacks will have access to the file name. In your specific case, in callback 2, you would have

img = imread(handles.filename);

Of course, you might want to use this image later on in another function, so you can store it in handles too

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