Thứ Tư, 17 tháng 9, 2014

Create a global variable in a GUI

Ask: 
Im making a gui which has a two buttons:

function button_browse_Callback(hObject, eventdata, handles)
         filename = uigetfile
function button_load_Callback(hObject, eventdata, handles)
        fid=fopen(filename,'rt'); ....etc

but I cant do the above because button_load_Callback does not have access to filename. How can I pass the filename from button_browse_callback to button_load_callback? ====================================
Answer by Azzi Abdelmalek on 28 Sep 2013
You don't need global variables, use guidata.

function button_browse_Callback(hObject, eventdata, handles)
          filename = uigetfile ...
          handles.filename=filename guidata(hObject,handles) %----------------------------------------------------------------------------
 function button_load_Callback(hObject, eventdata, handles)
         filename=handles.filename fid=fopen(filename,'rt'); ... ==================================================
Answer by Image Analyst on 28 Sep 2013
Another way is to declare the variables global in every function that needs to access them.

function button_browse_Callback(hObject, eventdata, handles)
         global baseFileName folder;
         [baseFileName, folder] = uigetfile();

function button_load_Callback(hObject, eventdata, handles)
         global baseFileName folder;
         fullFileName = fullfile(folder, baseFileName);
         fid=fopen(fullFileName ,'rt');

 Or see the FAQ:
 http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
 http://matlab.wikia.com/wiki/FAQ#Are_global_variables_bad.3F

1 nhận xét:

  1. The best slot machines in Vegas
    Slots - The Best Slot Machines in Vegas. 룰렛 규칙 Vegas 스 크릴 casinos always 사다리 게임 사이트 offer the 가입시 꽁머니 사이트 most immersive and authentic slots machines in Vegas. Enjoy the top 강원 랜드 앵벌이 Las

    Trả lờiXóa