Store a variable from a GUI action

4 views (last 30 days)
Fer
Fer on 19 Jun 2015
Commented: Walter Roberson on 19 Jun 2015
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

Answers (1)

Walter Roberson
Walter Roberson on 19 Jun 2015
  2 Comments
Fer
Fer on 19 Jun 2015
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 on 19 Jun 2015
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);

Sign in to comment.

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!