Store a variable from a GUI action

조회 수: 3 (최근 30일)
Fer
Fer 2015년 6월 19일
댓글: Walter Roberson 2015년 6월 19일
Hello. I'm designing a GUI for an image processing application. On pushing a button, I need an action to be executed and also a variable to be stored. The exectuted action is uigetfile to obtain the path to a file. The variable is the path to the file so my program accesses that file later. I suppose it is a string(?). I tried getting that variable like I would in a regular function:
function [file_path]=pushbutton1_Callback(hObject, eventdata, handles)
but I suppose that's not the way to do it. I found I should specify the value of a variable when using assignin as:
assignin(WS, name, value)
but how do I specify this if it's not numerical? Thank you

답변 (1개)

Walter Roberson
Walter Roberson 2015년 6월 19일
  댓글 수: 2
Fer
Fer 2015년 6월 19일
I didn't mean a value, what i meant is: the action triggered by pushing the button will obtain the path to a file. I successfully used assign in to save this path to the workspace, but these variables do not seem accessible for other functions in the GUI. My code:
% --- Executes on button press in pushbutton1.
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)
%%Open a dialog box requesting the image file
[img_name, img_path]=uigetfile('*.nii', 'Select the image');
%Concatenate path and name for future reference
img_path_name=[img_path, img_name];
assignin('base', 't1_path', img_path_name);
obtains the variable 't1_path'. Which I would then want to be available as an input for a function called by another button:
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
t1_path=evalin('base', t1_path);
displayDecreaseFA(t1_path);
but running the program says t1_path is undefined.
Walter Roberson
Walter Roberson 2015년 6월 19일
t1_path = evalin('base', 't1_path');
But Don't Do That.
% --- Executes on button press in pushbutton1.
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)
%%Open a dialog box requesting the image file
[img_name, img_path] = uigetfile('*.nii', 'Select the image');
%Concatenate path and name for future reference
img_path_name = fullfile(img_path, img_name);
handles.t1_path = img_path_name;
guidata(hObject, handles);
and
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
t1_path = handles.t1_path;
displayDecreaseFA(t1_path);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by