error in writing values to text file

4 ビュー (過去 30 日間)
nkumar
nkumar 2014 年 10 月 8 日
回答済み: Guillaume 2014 年 10 月 8 日
I have a code below
clc
clear all
close all
% a= imread('im16_1a.png');
% I=rgb2gray(a);
I=imread('rice.png');
imgTrans = I';
% iD conversion
img1D = I(:);
% Decimal to Hex value conversion
imgHex = dec2hex(img1D,6);
% New txt file creation
fid = fopen('text.txt', 'wt');
% Hex value write to the txt file
fprintf(fid,'%s\n',imgHex)
% Close the txt file
fclose(fid)
winopen('text.txt')
i this i have converted to hexadecimal,i want to write values in imgHex to text file,but wen i write i dont get vales as in imgHex,kindly help

採用された回答

Guillaume
Guillaume 2014 年 10 月 8 日
dec2hex returns a single 2d string which is then flattened by column into a 1d string in the call to fprintf. You have several options. one is to iterate over the rows of the 2d string and call fprintf on each row:
for hexrow = 1:size(imgHex, 1)
fprintf(fid, '%s\n', imgHex(hexrow, :));
end
Another is:
cellfun(@(hexstring) fprintf(fid, '%s\n', hexstring), num2cell(imgHex, 2));

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by