Generating a text file and autofill it with outputs

조회 수: 6 (최근 30일)
Robu Robert
Robu Robert 2021년 4월 24일
댓글: Robu Robert 2021년 4월 24일
Hello! Is there a way to generate a .txt file and fill it with strings, one on each row, in a for loop? To be exact, in a for loop i generate different outputs and i want to save all of them in a .txt file but i don't know how.
for i=1:length(text)
linie=find(key(i)==alfabet);
coloana=find(text(i)==mat(linie,:));
decrypt=[decrypt mat(1,coloana)];
end
decrypt

채택된 답변

Clayton Gotberg
Clayton Gotberg 2021년 4월 24일
편집: Clayton Gotberg 2021년 4월 24일
Yes!
fid = fopen('output_file.txt','wt'); % Open output_file.txt
% 'w' means overwrite everything that is already in the file, use 'a' to
% append instead. 't' means format more like a text file
for i=1:length(text)
linie=find(key(i)==alfabet);
coloana=find(text(i)==mat(linie,:));
decrypt=[decrypt mat(1,coloana)];
fprintf(fid,'%s\n',coloana); % To the file already opened (identified by fid)
% write a single line (%s) followed by a newline symbol (\n)
% '%s' tells the function to expect a string input; if the input
% you want to write to the file is not a string you'll need to change
% the formatting identifier. A link to the function is below.
end
decrypt
fclose(fid) % close the opened file
Functions: fopen, fprintf (direct link to formatting inputs), fclose

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by