GUI Pushbutton, display user input?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Caitlin Taylor
el 12 de Jun. de 2018
Comentada: Caitlin Taylor
el 12 de Jun. de 2018
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 comentarios
Respuesta aceptada
Rik
el 12 de Jun. de 2018
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
Más respuestas (0)
Ver también
Categorías
Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!