Domanda

I have a lot of images that are in a directory named 1.jpg, 2.jpg, 3.jpg etc. I read them one by one. I do some operation & then I save them.

I want to automate this operation. I can read the image names. Then while generating the output file I extract the image_name from the input file name, add my required extension names, add the type of file I want to save and then save the images by print command.

%//Read the image
imagefiles = dir('*.bmp');      
nfiles = length(imagefiles);    % Number of files found
for ii=1:nfiles
    currentfilename = imagefiles(ii).name;
    currentimage = imread(currentfilename);
    images{ii} = currentimage;
    Img=currentimage;

    %//Do some operation on the image

    %//Save the image file
    h=figure;
    %//Display the figure to be saved  
    token = strtok(currentfilename, '.');
    str1 =  strcat(token,'_op');
    print(h,'-djpeg',str1);
end

This program works fine but then I found out about this command to plot beautiful graphs. export_fig

export_fig takes the basic command in the form of:

export_fig file_name.file_type

How can I substitute my output file name that is stored as str1 in place of file_name place-holder in the export_fig command automatically.

NOTE: Please note this from the export_fig documentation (for variable file names)

for a = 1:5
plot(rand(5, 2));
export_fig(sprintf('plot%d.png', a));
end

I do not want this solution. Please understand my query that there are thousands of MATLAB functions that requires data to be inputted as given in export_fig basic statement. The special case regarding variable file names have been already built within the export_fig function.

I want to know if it was not built, then how could I have used automatically generated variable file names? My query is not specifically regarding export_fig but regarding the basic way in which I can supply variable file names if the input cannot be a string?

Please ask me if you have trouble understanding the question.

È stato utile?

Soluzione

The syntax my_function file_name.file_type is equivalent to my_function('file_name.file_type') - there is no difference between the two.

So if you want this in a loop, you can use whatever method to create the filename and then call the function:

for i=1:N
    % construct the filename for this loop - this would be `str1` in your example
    file_name = sprintf('picture_%i.jpeg', i);
    % or:
    file_name = strcat('picture_', num2str(i), '.jpeg');
    % call the function with this filename:
    my_function(file_name);
end
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top