Using fprintf to print in text file

28 views (last 30 days)
Sang Heon Lee
Sang Heon Lee on 29 Sep 2017
Edited: OCDER on 29 Sep 2017
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

Answers (2)

Cedric
Cedric on 29 Sep 2017
Edited: Cedric on 29 Sep 2017
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 on 29 Sep 2017
Edited: OCDER on 29 Sep 2017
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 Comments
Jan
Jan on 29 Sep 2017
Edited: Jan on 29 Sep 2017
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 on 29 Sep 2017
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.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!