How do i write a text..

I wrote this code and i would like to write a text on the top of this file and on the bottom. How can i do this?
grayImage = imread('moon.tif');
[height, width] = size(grayImage)
[X, Y] = meshgrid(1:width, 1:height);
numberOfPixels = length(X(:))
points = [X(:) Y(:) grayImage(:)];
A = [X(:) Y(:) grayimage(:)];
fid = fopen('moon.txt','wt');
for ii = 1:size(A,1)
fprintf(fid,'%g\t',A(ii,:));
fprintf(fid,'\n');
end
fclose(fid)

 採用された回答

Image Analyst
Image Analyst 2013 年 6 月 8 日
編集済み: Image Analyst 2013 年 6 月 8 日

0 投票

Simply add a fprintf() before and after you write the gray levels to the text values.
fid = fopen('moon.txt','wt');
fprintf(fid,'Stuff at the beginning of the file.\n');
for ii = 1:size(A,1)
fprintf(fid,'%g\t',A(ii,:));
fprintf(fid,'\n');
end
fprintf(fid,'Stuff at the end of the file.\n');
fclose(fid)

5 件のコメント

George
George 2013 年 6 月 8 日
I would like to write all of them in the same file e.g.
Stuff at the beginning of the file
1 1 255
1 2 200
Stuff at the end of the file
Image Analyst
Image Analyst 2013 年 6 月 8 日
Why is it that you think the text lines are written to different files than the pixel values? They aren't. They ARE written to the same file.
George
George 2013 年 6 月 8 日
i write this code and when i open the moon.txt file i see only the pixel values
grayImage = imread('moon.tif');
[height, width] = size(grayImage)
[X, Y] = meshgrid(1:width, 1:height);
numberOfPixels = length(X(:))
points = [X(:) Y(:) grayImage(:)];
A = [X(:) Y(:) grayImage(:)];
fid = fopen('moon.txt','wt');
fprintf('Stuff at the beginning of the file.\n');
for ii = 1:size(A,1)
fprintf(fid,'%g\t',A(ii,:));
fprintf(fid,'\n');
end
fprintf('Stuff at the end of the file.\n');
fclose(fid)
Image Analyst
Image Analyst 2013 年 6 月 8 日
Sorry - forgot to add that you need to pass in the file ID. See corrected code. If you don't have that it just prints to the command window.
George
George 2013 年 6 月 8 日
thank you for your time!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by