2014-10-10 96 views
-1

我想加載一個文本文件絲毫ASCII字符,把所有的內容到MATLAB一個varialbe,這是代碼我想:插入一個文本文件的內容到一個變量

f =fopen('tp.txt') 

結果我得到的是1,那麼它每次執行此代碼行時都會增加。

然而,當我嘗試:

f =load('tp.txt') 

我得到這個錯誤:

??? Error using ==> load 
Number of columns on line 1 of ASCII file D:\Cours\TP\tp.txt 
must be the same as previous lines. 

Error in ==> TPTI at 3 
f =load('tp.txt') 

我的目標是計算每個charachter然後計算propabilites和熵的次數。

有什麼想法?

+0

是否ASCII文件包含一行或幾行?你想把這些內容作爲一個字符串放入一個變量嗎?作爲一個字符串數組? – 2014-10-10 19:49:23

+0

該文件包含多行。我想把內容放入一個字符串變量中。 – Somar 2014-10-10 19:50:58

回答

2

試試這個:

%// Import file contents as a string 
str = importdata('tp.txt'); %// each line is a cell 
str = [str{:}]; %// concat all lines into a single string 

%// Remove spaces. Maybe you want to remove other characters as well: punctuation... 
str = regexprep(str, '\s', ''); 

%// Count frequency of each character: 
freq = histc(double(str), 0:255); %// convert characters to ASCII codes and count 
+0

謝謝,它工作:) – Somar 2014-10-10 20:04:23

相關問題