如何从matlab中相同m文件中的函数中获取变量值?

时间:2023-01-30 20:53:23

I want to make a GUI in matlab. In the (*.fig) file, There will be 2 push buttons. the first is to read the image from the directory, save it into a variable and show it in the matlab GUI.

我想在matlab中创建一个GUI。在(* .fig)文件中,将有2个按钮。第一种是从目录中读取图像,将其保存到变量中并在matlab GUI中显示。

so in the main file (*.m) i will have 2 functions that represent the 2 buttons.

所以在主文件(* .m)中我将有2个代表2个按钮的函数。

for example in the main file (*.m), the first function : x=imread('image.bmp');

例如在主文件(* .m)中,第一个函数:x = imread('image.bmp');

And the second button (the second function, in the same main file as a first function) is to make a process with a image that have read by the first button. so, i have to get the variable x from the first button (function) to do the process. If i have get the x variable, i can send it to another *.M files.

并且第二个按钮(第二个功能,在与第一个功能相同的主文件中)是使用第一个按钮读取的图像进行处理。所以,我必须从第一个按钮(函数)获取变量x来执行该过程。如果我得到x变量,我可以将它发送到另一个* .M文件。

How to get the x variable from the first function.

如何从第一个函数中获取x变量。

Here is my code..

这是我的代码..

THE MAIN FILE

%First button (Function)
function pushbutton1_Callback(hObject, eventdata, handles)  
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[filename, pathname] = uigetfile({'*.bmp';'*.jpg';'*.gif';'*.*'},        
'Pick an Image File');
citra1 = imread([pathname,filename]);
axes(handles.axes1);
imshow(citra1);
handles.citra1 = citra1;
guidata(hObject, handles);
set(handles.text1,'String',filename);

% and here is my second button (function)

%,这是我的第二个按钮(功能)

function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[gambr1, psan1, jmlobjk1, brs1, klm1]=BacaCitra1(citra1);
 axes(handles.axes3);
 imshow(gambr1);
 handles.gambr1 = gambr1;
 guidata(hObject, handles);

i wanna send the "citra1" as a image variable from the first function to be read in the second function, so i can do

我想发送“citra1”作为第二个函数中要读取的第一个函数的图像变量,所以我可以做

 [gambr1, psan1, jmlobjk1, brs1, klm1]=BacaCitra1(citra1);

but there are error messeges like : Error while evaluating uicontrol Callback

但有错误信息如:评估uicontrol回调时出错

??? Undefined function or variable 'citra1'.
Error in ==> deteksi2citra>pushbutton1_Callback at 117
[gambr1, psan1, jmlobjk1, brs1, klm1]=BacaCitra1(citra1);

 Error in ==> gui_mainfcn at 96
    feval(varargin{:});

  Error in ==> deteksi2citra at 42
gui_mainfcn(gui_State, varargin{:});

 Error in ==>        @(hObject,eventdata)deteksi2citra('pushbutton1_Callback',hObject,even data,guidata(hObject))
 ??? Error while evaluating uicontrol Callback

Thank you for the help :D

谢谢你的帮助:D

1 个解决方案

#1


0  

It looks like you are already doing what you need to do, in fact, it looks like you are using two methods in parallel. You save the variable citra1 to the handles structure in pushbutton1_Callback and you are accepting that structure as a user variable in both callbacks. You are also saving handles with guidata.

看起来你已经在做你需要做的事了,事实上,看起来你正在并行使用两种方法。您将变量citra1保存到pushbutton1_Callback中的句柄结构中,并且您在两个回调中都接受该结构作为用户变量。您还使用guidata保存句柄。

See Sharing Data Among a GUI's Callbacks and guidata to see how you to extract citra1 from handles in either case.

请参阅在GUI的回调和guidata*享数据,以了解如何从句柄中提取citra1。

If you are setting the callbacks correctly, including the handles structure, then the short answer is to use handles.citra1 instead of just citra1 in your second callback. I highly recommend reading the callback documentation to understand what is happening, however.

如果你正确设置回调,包括句柄结构,那么简短的回答是在你的第二个回调中使用handles.citra1而不是citra1。但我强烈建议您阅读回调文档以了解正在发生的事情。

#1


0  

It looks like you are already doing what you need to do, in fact, it looks like you are using two methods in parallel. You save the variable citra1 to the handles structure in pushbutton1_Callback and you are accepting that structure as a user variable in both callbacks. You are also saving handles with guidata.

看起来你已经在做你需要做的事了,事实上,看起来你正在并行使用两种方法。您将变量citra1保存到pushbutton1_Callback中的句柄结构中,并且您在两个回调中都接受该结构作为用户变量。您还使用guidata保存句柄。

See Sharing Data Among a GUI's Callbacks and guidata to see how you to extract citra1 from handles in either case.

请参阅在GUI的回调和guidata*享数据,以了解如何从句柄中提取citra1。

If you are setting the callbacks correctly, including the handles structure, then the short answer is to use handles.citra1 instead of just citra1 in your second callback. I highly recommend reading the callback documentation to understand what is happening, however.

如果你正确设置回调,包括句柄结构,那么简短的回答是在你的第二个回调中使用handles.citra1而不是citra1。但我强烈建议您阅读回调文档以了解正在发生的事情。