send data to an output file (.txt) using fprintf
2 次查看(过去 30 天)
显示 更早的评论
Hey everyone,
I want to generate and send data to a text file using fprintf command and I want my output to look like this:
120 x1 + 120 x2 + 120 x3 + 120 x4 + 120 x5 + 120 x6 + 120 x7 + 120 x8 + 120 x9 + 120 x10 + 120 x11 + 120 x12 + 120 x13 + 120 x14 + 120 x15 + 120 x16 + 120 x17 + 120 x18 + 120 x19 + 120 x20 + 40 y1 + 40 y2 + 40 y3 + 40 y4 + 40 y5 + 40 y6 + 40 y7 + 40 y8 + 40 y9 + 40 y10 + 40 y11 + 40 y12 + 40 y13 + 40 y14 + 40 y15 + 40 y16 + 40 y17 + 40 y18 + 40 y19 + 40 y20 + 40 y21 + 40 y22 + 40 y23 + 40 y24 + 40 y25 + 40 y26 + 40 y27 + 40 y28 + 40 y29 + 40 y30 + 40 y31 + 40 y32 + 40 y33 + 40 y34 + 40 y35 + 40 y36 + 40 y37 + 40 y38 + 40 y39 + 40 y40 + 40 y41 + 40 y42 + 40 y43 + 40 y44 + 40 y45 + 40 y46 + 40 y47 + 40 y48 + 40 y49 + 40 y50 + 40 y51 + 40 y52 + 40 y53 + 40 y54 + 40 y55 + 40 y56 + 40 y57 + 40 y58 + 40 y59 + 40 y60
So I wrote this code:
fid = fopen('Scenario.txt', 'w');
fprintf(fid,'Minimize');
fprintf(fid,'\r\n');
c1 = 120; %$ 120,000
c2 = 40; %$ 40,000
for i= 1:length(x)
for j=1:length(y)
% c1*x(i)
% fprintf(fid,'');
end
end
fprintf(fid,'\r\n'); fclose(fid);
I got stuck in the for loop part; how can I use the fprintf command to display my intended output??
0 个评论
采纳的回答
Robert Cumming
2011-6-5
fprintf ( fid, '%i', c1 ); % will write "120" to your file
fprintf ( fid, '%i x%i', c1, i ); % will write "120 x1"
that should be enough to give you an idea how to progress
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!