How to use the variables created within the GUI button function in other functions? (How to call the handles structure?)
1 view (last 30 days)
Show older comments
global lstBox;
inputfolder = uicontrol('Style','pushbutton','String','InputFolder', 'Position', [4 2 15 4.5], @inputfoler_callback);
lstBox = uicontrol('Style','listbox','Units','Normalized','Position', [0.06 0.03 0.9 0.95], @lstBox_callback);
function inputfolder_callback(hObject,eventdata,handles)
folderPath = uigetdir('','inputpath');
imgFiles = dir(fullfile(folderPath, '**\*.png'));
testRemove = contains({imgFiles.name},'test');
imgFiles(testRemove) = [];
imgNames = {imgFiles.name};
handles.imgFiles = imgFiles;
set(lstBox, 'String', []);
set(lstBox, 'String', imgNames);
set(lstBox, 'Value', 1);
guidata(hObject, handles);
end
function lstBox_callback(hObject,eventdata,handles)
files = handles.imgFiles;
path = files(1).folder + "\" + files(1).name;
end
This code attempts to load a png image file into the path selected with the Inputfolder button and write a code indicating the png file name in the lstBox.
Save the structure of the .png file in the 'imgFiles' variable
"testRemove" removes the value with the name "test" and saves only the file name in "imgNames".
And store the imgFiles in the 'handles' structure.
From the listBox_callback function, you are trying to open an image by retrieving the hands.imgFiles in the hands
The error message files = hands.imgFiles; Insufficient input arguments in this code.
So what I'm curious about in this code is
1. How to use variables saved in handles in other functions?
- Insufficient input arguments error
2. I'm trying to output a png file name to lstBox, how to call lstBox within a function without specifying the lstBox variable as global?
1 Comment
Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps 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!