Azzera filtri
Azzera filtri

Using fprintf to print in text file

4 visualizzazioni (ultimi 30 giorni)
Sang Heon Lee
Sang Heon Lee il 29 Set 2017
Modificato: OCDER il 29 Set 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

Risposte (2)

Cedric
Cedric il 29 Set 2017
Modificato: Cedric il 29 Set 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 il 29 Set 2017
Modificato: OCDER il 29 Set 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 Commenti
Jan
Jan il 29 Set 2017
Modificato: Jan il 29 Set 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 il 29 Set 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.

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by