GUI Pushbutton, display user input?
2 次查看(过去 30 天)
显示 更早的评论
Hi, I wrote the following code to pull out a user defined increment of photos and file those photos somewhere else (user defined) for DIC. For the pushbutton with the uigetdir prompt, I would like it to orginally say "Directory" or something, then once the user pushes the button the user navigates to their desired directory, then I would like the pushbutton to display what directory the user chose. How do I do this?
%In order to work the image files of interest must be in the current
%path/directory. They also must be in the .jpg format.
clear all;
clc;
f= figure;
selpath = 'Directory';
uicontrol(f,'style', 'text','string', 'Enter Index','position', [65,355,100,20]);
cinput = uicontrol(f,'style', 'edit','String','#','position', [100,325,30,30],'callback','c = get(cinput);C=c.String;I=str2num(C);');
uicontrol(f,'style', 'text','string', 'Enter New Folder Name','position', [300,355,120,20]);
binput = uicontrol(f,'style', 'edit','string', 'NewFolderName','position', [260,325,200,30],'callback','b = get(binput);B=b.String;');
bdinput = uicontrol(f,'style', 'pushbutton','string', selpath,'position', [100,255,360,30], 'callback', 'selpath = uigetdir;');
uicontrol(f,'style', 'pushbutton','string', 'Done','position', [460,200,40,30], 'callback', 'closereq');
uiwait(f);
imagefiles = dir('*.jpg');%Find the jpg files
nfiles = length(imagefiles);%How many files are there
if nfiles ==0 %If there are no .jpg files found output a message
message1= sprintf('Please add your image folder to the current directory navigation bar.');
msgbox(message1);
end
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
currentimage = imread(currentfilename);
images{ii} = currentimage;
end
for jj = 1:I:nfiles
bestdirectory = strcat(selpath,'\',B);%choosen by user
mkdir(bestdirectory);
j=['Image_S001_' num2str(jj-1,'%04d') '.jpg'];
fulldestination = fullfile(bestdirectory,j); %name file relative to that directory
q=images{jj};
imwrite(q, fulldestination); %save the file there directory
end
0 个评论
采纳的回答
Rik
2018-6-12
You should read up on guidata to read up on how to share data between callbacks of a GUI. A callback is not run when you create it, only when you click it.
A tip: use multiple lines if you define multiple Name,Value pairs, it really improves readability.
bdinput = uicontrol(f,...
'style', 'pushbutton',...
'string', 'Directory',...
'position', [100,255,360,30],...
'callback', 'selpath = uigetdir;[~,seldir]=fileparts(selpath);set(gcbo,''String'',seldir)');
Another option:
bdinput = uicontrol(f,...
'style', 'pushbutton',...
'string', 'Directory',...
'position', [100,255,360,30],...
'callback',@bdinput_callback);
%sometimes people use this:
%'callback',@(hObject,eventdata)bdinput_callback(hObject,eventdata,guidata(hObject))
%function bdinput_callback(hObject,eventdata,handles) %#ok<INUSD>
function bdinput_callback(hObject,eventdata) %#ok<INUSD>
handles=guidata(hObject);%load from shared guidata
selpath = uigetdir;
[~,seldir]=fileparts(selpath);
set(gcbo,'String',seldir)
handles.seldir=seldir;
guidata(hObject,handles)%save back to shared guidata
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!