问题
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.
- 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)
回答1:
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);
This way you can easily access each line with txtlines{ii}
.
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.
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);
回答2:
If the file is reasonably sized (most 30 line files are) I would read it all into memory at once.
fid = fopen('saturate.m');
str = fread(fid,inf,'*char')';
fclose(fid);
Then, depending on your needs you can use basic matrix operations, string operations or regexp style analysis on the str
variable.
For example, "how many capital 'I''s?" is:
numIs = sum(str=='I');
Or, "how many instances of 'someString'?" is:
numSomeString = length(strfind(str, 'someString'));
来源:https://stackoverflow.com/questions/13755048/matlab-txt-file-analyses