在matlab中写入文本文件
Writing to text file in matlab
我想对图片进行一些操作,并将一些数据写入一个txt file.Here 就是我对一张图片所做的-
clc;
image=imread('im.png');
.... %do some operations
....
....
fileID=fopen('first.txt','w');
.... %write onto the txt file
....
fclose(fileID);
但我想这样做,因为许多 images.I 已将所有图像存储在一个 folder.Also 中,我想在上一个中断的地方立即继续写入同一个文本文件image.How 我可以修改我的代码来实现这个吗?
嗯,这很简单。使用循环遍历所有图像,对其进行处理,然后附加到文本文件。更简单的是,您只需打开一次文本文件,有图片就写多少次,最后关闭它。
像这样:
folder = ...; %// Place folder here - Example: folder = fullfile('D:', 'images');
fileID=fopen('first.txt','w'); %// Open up the file for writing
f = dir(fullfile(folder, '*.png')); %// look for all PNG files in this folder
for idx = 1 : numel(f)
filename = fullfile(folder, f(idx).name); %// Get the file name
im = imread(filename); %// Read the image in
.... %do some operations
....
....
.... %write onto the txt file
....
fprintf(fileID, '\n\n'); %// Put two carriage returns to make way for next file
end
fclose(fileID);
函数dir
scans for all files that match a particular expression. In your case, you want to find all PNG files in a particular folder of your choosing. I assume that this is stored in the variable folder
. We then open up the file first before we do anything to the images, then loop over each of the found images with dir
. Take note that when you use dir
, it only finds the relative paths to the files (i.e. just the names themselves). If you want to locate where the actual images are, you need the absolute paths, which is why we use fullfile
。
因此,对于文件夹中的每个 PNG 图像,加载它,对其进行处理,写入您的文件,我确保放入两个回车符 returns 来分隔每个结果。对每个 PNG 图像重复此操作,直到从文件夹中耗尽所有图像。完成后,关闭文本文件。
小注
image
是 MATLAB 中的一个实际函数,它将值矩阵可视化为具有指定颜色图的图像。您可能应该将此变量重命名为其他名称,以免在您编写的其他脚本/函数使用此函数时掩盖该函数。
我想对图片进行一些操作,并将一些数据写入一个txt file.Here 就是我对一张图片所做的-
clc;
image=imread('im.png');
.... %do some operations
....
....
fileID=fopen('first.txt','w');
.... %write onto the txt file
....
fclose(fileID);
但我想这样做,因为许多 images.I 已将所有图像存储在一个 folder.Also 中,我想在上一个中断的地方立即继续写入同一个文本文件image.How 我可以修改我的代码来实现这个吗?
嗯,这很简单。使用循环遍历所有图像,对其进行处理,然后附加到文本文件。更简单的是,您只需打开一次文本文件,有图片就写多少次,最后关闭它。
像这样:
folder = ...; %// Place folder here - Example: folder = fullfile('D:', 'images');
fileID=fopen('first.txt','w'); %// Open up the file for writing
f = dir(fullfile(folder, '*.png')); %// look for all PNG files in this folder
for idx = 1 : numel(f)
filename = fullfile(folder, f(idx).name); %// Get the file name
im = imread(filename); %// Read the image in
.... %do some operations
....
....
.... %write onto the txt file
....
fprintf(fileID, '\n\n'); %// Put two carriage returns to make way for next file
end
fclose(fileID);
函数dir
scans for all files that match a particular expression. In your case, you want to find all PNG files in a particular folder of your choosing. I assume that this is stored in the variable folder
. We then open up the file first before we do anything to the images, then loop over each of the found images with dir
. Take note that when you use dir
, it only finds the relative paths to the files (i.e. just the names themselves). If you want to locate where the actual images are, you need the absolute paths, which is why we use fullfile
。
因此,对于文件夹中的每个 PNG 图像,加载它,对其进行处理,写入您的文件,我确保放入两个回车符 returns 来分隔每个结果。对每个 PNG 图像重复此操作,直到从文件夹中耗尽所有图像。完成后,关闭文本文件。
小注
image
是 MATLAB 中的一个实际函数,它将值矩阵可视化为具有指定颜色图的图像。您可能应该将此变量重命名为其他名称,以免在您编写的其他脚本/函数使用此函数时掩盖该函数。