Search code examples
arraysmatlabvectorimread

In MatLab, how can I create a character vector from a cell array?


I am clearly having trouble distinguishing between the different types of vectors and arrays and commands like string, cellstr, char, etc. My code is on another network but basically, I get an error saying the argument in my imread statement must be a character vector. The argument is a 1x30 array of file names and is a cell array because I used iscell command and it came back with a 1. I have tried several combos of the commands listed above and have been reading everything I can but cannot determine how to change the 1x30 cell array to a character vector so the imread statement will work. The file names are read in from a folder (using uigetfile) as 757-01.bmp, 757-02.bmp ... 757-30.bmp. I assume I need to make them '757-01.bmp', '757-02.bmp' ... '757-30.bmp' and possibly into a 30x1 vector vice 1x30? Or perhaps that doesn't matter for the for loop the code will encounter next..? Thank you for any assistance...

[imagefiles,file_path]=uigetfile({'*.jpeg;*.jpg;*.bmp;*.tif;*.tiff;*.png;*.gif','Image Files (JPEG, BMP, TIFF, PNG and GIF)'},'Select Images','multiselect','on');
imagefiles = cellstr(imagefiles);
imagefiles = sort(imagefiles);
nfiles = length(imagefiles);

r = inputdlg('At what pixel row do you want to start the cropping?        .','Row');
r = str2num(r{l});

c = inputdlg('At what pixel column do you want to start the cropping (Must be > 0)?        .','Column');
c = str2num(c{l});

h = inputdlg('How many pixel rows do you want to include in the crop?        .','Height');
h = str2num(h{l});

w = inputdlg('How many pixel columns do you want to include in the crop?        .','Width');
w = str2num(w{l});

factor = inputdlg('By what real number factor do you want to enlarge the cropped image?        .','Factor');
factor = str2num(factor{l});

outdimR = h * factor;
outdimC = w * factor;

for loop=l:nfiles
    filename = imagefiles(loop);
    [mybmp, map] = imread(filename);
    myimage = ind2rgb(mybmp,map);
    crop = myimage(r:r+h-1, c:c+w-1, :);
    imwrite(crop, sprintf('crop757-%03i.bmp',loop));
end

Solution

  • Regarding your original question:

    In MatLab, how can I create a character vector from a cell array?

    You can use cell accessor operands ({}) to get the character string from your cell as @ben-voight pointed out, or alternatively wrap char() around your statement (I'd go with Ben's suggestion).

    Regarding your follow-on issue:

    It errors out at imread saying File "757-01.bmp" does not exist. In the imagefiles array, the first of 30 values is 757-01.bmp (no quotes) but I don't know if MatLab putting quotes around the file name means its looking for a value with quotes in the array or not.

    It sounds like your file is in another directory than the one you are running your code from.

    Use fullfile to create the full filename to use the file's absolute path instead of a relative path (which isn't working here).

    Let's say your files in are in the path ~/bpfreefly/images/.

    Then you can change your code like this:

    imgPath = '~/bpfreefly/images/'
    for loop=l:nfiles
        filename = fullfile(imgPath,imagefiles{loop});
        [mybmp, map] = imread(filename);
        myimage = ind2rgb(mybmp,map);
        crop = myimage(r:r+h-1, c:c+w-1, :);
        imwrite(crop, sprintf('crop757-%03i.bmp',loop));
     end
    

    By the way, you can get the pathname from uigetfile's second output argument.