2014-10-10 2 views
-1
내가 텍스트 파일 오순절 ASCII 문자를로드하고 MATLAB에서 varialbe에 모든 콘텐츠를 넣을

, 이것은 코드에 텍스트 파일의 내용을 삽입 나는 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') 

내 목표는 다음 propabilites와 엔트로피를 계산 각 charachter의 occurance을 계산하는 것입니다.

아이디어가 있으십니까?

+0

이 ASCII 파일은 한 줄 또는 여러 줄이 포함되어 있습니까? 그 내용을 문자열로 변수에 넣으려고합니까? 문자열의 배열로? –

+0

파일에 여러 줄이 포함되어 있습니다. 내용을 문자열 변수에 넣고 싶습니다. – Somar

답변

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