Matlab .txt文件分析 [英] Matlab .txt file analyses
问题描述
我正在分析.txt文件中的一组文本.该文件有30行,每行包含不同的短语,均包含文本,数字和符号.
I am analyzing a set of text in a .txt file. the file has 30 lines and each line contains different phrases both containing text, numbers, and symbols.
- 将文件导入Matlab进行分析的最佳方法是什么 (即:文本文件中有多少个大写字母或文件中有多少个#text短语(分析每行上的推文)
- what's the best way to import this file into Matlab for analyses (i.e.: how many Capital I's are in the text file or how many #text phrases are in the file (analyzing tweets on each line)
推荐答案
I think you'd best read the file line-by-line and save each line in a cell of a cell array:
fid = fopen(filename);
txtlines = cell(0);
tline = fgetl(fid);
while ischar(tline)
txtlines{numel(txtlines)+1}=tline;
tline = fgetl(fid);
end
fclose(fid);
这样,您可以使用txtlines{ii}
轻松访问每一行.
This way you can easily access each line with txtlines{ii}
.
如果您始终需要对全文进行操作(即,整个文本文件中有多少个a,而不是每行),那么您当然可以将这些行放到一个变量中.
If you always need to perform operations on the complete text (ie how many a's in the whole text-file, and not per-line), you can of course just throw the lines together in a single variable.
执行每行操作,只需使用 cellfun cellfun ,例如计算大写字母"I"的数量:
Executing an operation on each line, can be done simply with cellfun, for example counting the number of capital 'I's:
capI_per_line = cellfun(@(str) numel(strfind(str,'I')),txtlines);
这篇关于Matlab .txt文件分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!