Using fprintf to print in text file

4 次查看(过去 30 天)
Sang Heon Lee
Sang Heon Lee 2017-9-29
编辑: OCDER 2017-9-29
This is my script and I am trying to use fprintf to print sth in other file. This is the script I have currently and this does not change anything in my output.txt file. How can I print 'f' in the text file??
function [] = words(input, output)
fid = fopen(input, 'r');
a = [];
while ~feof(fid)
line = fgetl(fid);
e = lower(line);
b = strsplit(e);
a = [a b];
end
c = unique(a);
e = transpose(c);
f = string(e);
kid = fopen(output, 'w');
fprintf(output,'%s\n',f);
end

回答(2 个)

Cedric
Cedric 2017-9-29
编辑:Cedric 2017-9-29
The first argument in the call to FPRINTF should not be the file name but the file handle that you get with FOPEN:
kid = fopen(output, 'w');
fprintf(kid,'%s\n',f);
Also, I don't know what f is, but it may not be a string. You'll have to check that. If it is a cell array of strings, you can STRCAT them or just update the call to FPRINTF as follows:
fprintf(kid,'%s\n',f{:});
where f{:} develops f as a comma separated list (CSL). Finally, you have to close the file at the end:
fclose(kid);

OCDER
OCDER 2017-9-29
编辑:OCDER 2017-9-29
New solution: works for words separated by non-letter characters (ex: dog, cat, "bird")
function extractWords(input, output)
fid = fopen(input, 'r');
txtchar = fscanf(fid, '%c'); %use fscanf to save everything as char array, incl space
fclose(fid);
txt = unique(regexpi(lower(txtchar), '[a-z]+', 'match'));
kid = fopen(output, 'w');
fprintf(kid,'%s\n', txt{:});
fclose(kid);
OLD SOLUTION - does not work for words followed by , . " (ex: dog, cat, "this")
function extractWords(input, output)
fid = fopen(input, 'r');
txt = textscan(fid, '%s');
txt = txt{1};
fclose(fid);
txt = unique(strrep(lower(txt), '.', '')); %Thanks Jan for this simpler code!
%txt = cellfun(@(x) strrep(x, '.', ''), txt, 'UniformOutput', false');
%txt = unique(cellfun(@lower, txt, 'UniformOutput', false)); %get unique, sorted words, lower case
kid = fopen(output, 'w');
fprintf(kid,'%s\n', txt{:});
fclose(kid);
General suggestions:
  • No need for function [] = words(...) if there's no output. function words(...) works.
  • Use textscan to extract all words from a text file
  • Use Matlab's concept of " comma separated list ", txt{:}, to print all cells at once via fprintf.
  • Label variables to have meaning, instead of a, b, c, d
  • Label functions with a verb so that you'll know in the future it's a function and not some word cell array. Ex: extractWords(input, output) is obvious it's a function that extracts words from input and saves to output. words(input, output) looks similar to an array of words, where words(1) and words(2) looks like a reference to the 1st word and 2nd word.
  2 个评论
Jan
Jan 2017-9-29
编辑:Jan 2017-9-29
strrep and lower operate on cell strings directly:
txtC = textscan(fid, '%s');
txt = txtC{1};
fclose(fid);
txt = strrep(x, '.', '');
txt = lower(txt);
Thios is nicer and faster than the cellfun application.
OCDER
OCDER 2017-9-29
Thanks for this suggestion! I keep forgetting these work on cell arrays too. I edited the previous answer, and provided a new one as my prev answer did not handle words separated by comma, quotation, numbers, etc.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by