Select multiple files in browser for conversion.

Hello, I have written a simple code for converting RGB bitmap's to 8 bit .bmp's and .pgm to 8 bit .bmp. The code is as follows,
clear all
close all
% Prompt for image
[image_file image_file_path image_file_filterindex] = uigetfile({'*.pgm;*.bmp'}, 'Select image for conversion to *.bmp')
% This breaks up the input file name (image_file) into the directory (pathstr)
% the file name without the extention (name) and extention (ext)
[pathstr, name, ext] = fileparts(image_file);
% Find file extensions (must be either bmp or pgm)
image_file_type=image_file(max(strfind(image_file, '.'))+1:end);
% Only performs conversion if correct file types given
if(image_file_type=='bmp')
image_data=imread([image_file_path image_file], image_file_type);
image_data_n = rgb2gray(image_data);
imwrite(image_data_n, [image_file_path name '.bmp'], 'bmp');
elseif (image_file_type=='pgm')
image_data=imread([image_file_path image_file], image_file_type);
imwrite(image_data, [image_file_path name '.bmp'], 'bmp');
end
It works as intended however I would like to be able to select multiple files in the initial browser window to be converted. I thought of writing a loop to call different files of a similar name however then I would have to change the name of the files, as the names are not necessarily similar.
So, how do i change the above code so that I can select multiple files for conversion?
Thank you for your time, BN

 採用された回答

Tom
Tom 2012 年 6 月 26 日

0 投票

uigetfile({'*.pgm;*.bmp'}, 'Select image for conversion to *.bmp','MultiSelect','On')
which will give you a cell array of file names and paths
you can then run these in a loop, so
for n=1:length(image_file)
[pathstr, name, ext] = fileparts(image_file{n});
...
end
FYI there are a couple of bits in your code you can simplify, for example you've written:
image_file_type=image_file(max(strfind(image_file, '.'))+1:end);
when you essentially already have that as in the 'ext' variable.

3 件のコメント

Brenden
Brenden 2012 年 6 月 26 日
This works however now I cannot select one image file I must select >1 files.
Tom
Tom 2012 年 6 月 26 日
is the problem that if you only select one then it's not in a cell?
if ~iscell(image_file)
image_file={image_file};
image_path={image_path};
end
Brenden
Brenden 2012 年 6 月 26 日
You got it! Thank you again, BN

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2012 年 6 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by