localpathname为当前路径,需要保存图片的位置,根据自己的实际路径设置,usedpathname用来保存打开的文件位置,方便下次打开文件对话框以上次的路径为默认路径。
global usedpathname;
global localpathname; localpathname='D:\Program Files\MATLAB\R2013a\bin\毕业设计\图片\';if ~ischar(usedpathname)[filename, pathname, filterindex] = uigetfile( ...{ '*.*', 'All Files (*.*)';'*.tif',' (*.tif)'; ... '*.png',' (*.png)';'*.bmp',' (*.bmp)'; ... }, ... 'Pick a file', ... 'MultiSelect', 'on');else [filename, pathname, filterindex] = uigetfile( ...{ '*.*', 'All Files (*.*)';'*.tif',' (*.tif)'; ... '*.png',' (*.png)';'*.bmp',' (*.bmp)'; ... }, ... 'Pick a file', ... 'MultiSelect', 'on',usedpathname);endusedpathname=pathname;pic=strcat(pathname,filename);pic=imread(pic);if size(pic,3)>1 pic=rgb2gray(pic); imwrite(pic,[ localpathname strtok(filename,'.') '.tif'])endclear pathname;