1

我有很多图像位于名为 1.jpg、2.jpg、3.jpg 等的目录中。我一一阅读。我做了一些操作,然后保存它们。

我想自动化这个操作。我可以读取图像名称。然后在生成输出文件时,我从输入文件名中提取 image_name,添加我需要的扩展名,添加我要保存的文件类型,然后通过打印命令保存图像。

%//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

这个程序工作得很好,但后来我发现了这个命令来绘制漂亮的图表。export_fig

export_fig采用以下形式的基本命令:

export_fig file_name.file_type

如何自动替换存储为 str1 的输出文件名代替 export_fig 命令中的 file_name 占位符。

注意:请注意 export_fig 文档中的这一点(对于变量文件名)

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

我不想要这个解决方案。请理解我的查询,即有数千个 MATLAB 函数需要输入export_fig基本语句中给出的数据。关于变量文件名的特殊情况已经在 export_fig 函数中构建。

我想知道如果它没有构建,那我怎么能使用自动生成的变量文件名呢?我的查询不是专门针对 export_fig 而是关于如果输入不能是字符串,我可以提供变量文件名的基本方式?

如果您在理解问题时遇到困难,请询问我。

4

1 回答 1

5

语法my_function file_name.file_type等价于my_function('file_name.file_type')- 两者没有区别。

因此,如果您希望在循环中使用它,您可以使用任何方法来创建文件名,然后调用该函数:

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
于 2013-09-20T20:26:32.973 回答