필터 지우기
필터 지우기

Using fprintf to print in text file

조회 수: 9 (최근 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