How to get text file from char

3 次查看(过去 30 天)
hi, i have a problem.ı want get text form char matrix, fprintf doesnt work, how to get text file form matrix "T".
clc;
clear;
tic
b = sprintf('0123456789');
d = 0;
T = char;
for iiiii = 1:10
for iiiiii = 1:10
for iiiiiii = 1:10
for iiiiiiii = 1:10
for iiiiiiiii = 1:10
for iiiiiiiiii = 1:10
for iiiiiiiiiii = 1:10
d = d + 1;
T(d,1:4) = '0534';
T(d,5) = b(iiiii);
T(d,6) = b(iiiiii);
T(d,7) = b(iiiiiii);
T(d,8) = b(iiiiiiii);
T(d,9) = b(iiiiiiiii);
T(d,10) = b(iiiiiiiiii);
T(d,11) = b(iiiiiiiiiii);
fprintf('%s\n',T(d,:));
end
end
end
end
end
end
end
toc

采纳的回答

Shubham Gupta
Shubham Gupta 2019-10-28
Try :
clc;
clear;
tic
fid = fopen('myFile.txt','w'); % open text file
b = sprintf('0123456789');
d = 0;
T = char;
for iiiii = 1:10
for iiiiii = 1:10
for iiiiiii = 1:10
for iiiiiiii = 1:10
for iiiiiiiii = 1:10
for iiiiiiiiii = 1:10
for iiiiiiiiiii = 1:10
d = d + 1;
T(d,1:4) = '0534';
T(d,5) = b(iiiii);
T(d,6) = b(iiiiii);
T(d,7) = b(iiiiiii);
T(d,8) = b(iiiiiiii);
T(d,9) = b(iiiiiiiii);
T(d,10) = b(iiiiiiiiii);
T(d,11) = b(iiiiiiiiiii);
fprintf(fid,'%s\n',[T(d,:),char([13,10])]); % print T in your text file indicated by fid
end
end
end
end
end
end
end
fclose all % close text files
toc
You will get a text file named 'myFile.txt' in your working directory. Let me know if you have doubts !
  2 个评论
TARIK YAMAN
TARIK YAMAN 2019-10-28
I fixed, thanks to you.
I wrote fprintf(fid,'%s\n',[T(d,:),char()]) instead of fprintf(fid,'%s\n',[T(d,:),char([13,10])]).
clc;
clear;
tic
b = sprintf('0123456789');
d = 0;
T = char;
fid = fopen('myFile.txt','w');
for iiiii = 1:10
for iiiiii = 1:10
for iiiiiii = 1:10
for iiiiiiii = 1:10
for iiiiiiiii = 1:10
for iiiiiiiiii = 1:10
for iiiiiiiiiii = 1:10
d = d + 1;
T(d,1:4) = '0534';
T(d,5) = b(iiiii);
T(d,6) = b(iiiiii);
T(d,7) = b(iiiiiii);
T(d,8) = b(iiiiiiii);
T(d,9) = b(iiiiiiiii);
T(d,10) = b(iiiiiiiiii);
T(d,11) = b(iiiiiiiiiii);
fprintf(fid,'%s\n',[T(d,:),char()]);
end
end
end
end
end
end
end
toc
fclose = fid;
Shubham Gupta
Shubham Gupta 2019-10-28
I am glad I could help. If this answer solved your problem, please consider to accept it !

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by