插入一个文本文件的内容到一个变量
问题描述:
我想加载一个文本文件丝毫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和熵的次数。
有什么想法?
答
试试这个:
%// 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
是否ASCII文件包含一行或几行?你想把这些内容作为一个字符串放入一个变量吗?作为一个字符串数组? – 2014-10-10 19:49:23
该文件包含多行。我想把内容放入一个字符串变量中。 – Somar 2014-10-10 19:50:58