Search code examples
stringmatlabfilenames

Giving a string variable as a filename in matlab


I am using the below mentioned code to get the file names of images according to their id's from images_1 text file as strings and use them to read the images from their directory

image_count=1;
for image_count=1:6
    file=fopen('D:\Academics\New folder\CUB_200_2011\images_1.txt','r');
    C = textscan(file, '%s');
    original_image=imread('D:\Academics\New folder\CUB_200_2011\images\%s','C{1}{2*(image_count)}');
    imshow(original_image)
end

I am able to get the file name but not able to use it. This code shows the following error-

'File "D:\Academics\New folder\CUB_200_2011\images\%s" does not exist.

I am a bit new to matlab, can anyone please help me.


Solution

  • To concatenate parts of your file path, you have to use fullfile

     imread(fullfile('D:\Academics\New folder\CUB_200_2011\images\',C{1}{2*(image_count)}))
    

    Strcat is an alternative, but fullfile takes care of having one file separator wherever needed.