1. 程式人生 > >用MATLAB將矩陣資料寫入txt檔案中,開啟亂碼原因

用MATLAB將矩陣資料寫入txt檔案中,開啟亂碼原因

MATLAB將資料寫入txt檔案中亂碼的原因,是將資料按照二進位制檔案寫入txt檔案,所以開啟會出現亂碼的情況,只需要把

fid1=fopen('piture.txt','w');

換成,就可以了

fid1=fopen('piture.txt','wt');

下面一條程式碼是將資料按照文字檔案寫入的,所以開啟不會亂碼

下面給出一個例子

clc
clear all
I=imread('lena.jpg');
I1=im2bw(rgb2gray(I));
imshow(I1)
fid1=fopen('piture.txt','wt');
I2=uint8(I1);
for i=1:1:512
    for j=1:1:512
       c=num2str(I2(i,j));
     fprintf(fid1,'%c\n',c);
    end
end
 fclose(fid1);
 
 fid1=fopen('piture.txt','rt');
 b=fscanf(fid1,'%c')
 fclose(fid1);