Unable to create text file for output

function opt = output2file(opt, state, pop, type, varargin)
% Function: opt = output2file(opt, state, pop, type, varargin)
% Description: Output the population 'pop' to file. The file name is
% specified by 'opt.outputfile' field.
% Parameters:
% type : output type. -1 = the last call, close the opened file.
% others(or no exist) = normal output
% varargin : any parameter define in the options.outputfuns cell array.
%
% LSSSSWC, NWPU
% Revision: 1.2 Data: 2011-07-13
%*************************************************************************
if(isempty(opt.outputfile))
return; % the output file name is not specified, return directly
end
if( isfield(opt, 'outputfileFID') )
fid = opt.outputfileFID;
else
fid = [];
end
%*************************************************************************
% 1.Open the output file and output some population info
%*************************************************************************
if( isempty(fid) )
fid = fopen(opt.outputfile, 'pops.txt', 'w');
if( fid == 0)
error('NSGA2:OutputFileError', 'Can not open output file!! file name:%s', opt.outputfile);
end
opt.outputfileFID = fid;
% Output some infomation
fprintf(fid, '#NSGA2\r\n');
% fprintf(fid, 'popsize %d\r\n', opt.popsize);
fprintf(fid, 'maxGen %d\r\n', opt.maxGen);
fprintf(fid, 'numVar %d\r\n', opt.numVar);
fprintf(fid, 'numObj %d\r\n', opt.numObj);
fprintf(fid, 'numCons %d\r\n', opt.numCons);
% Output state field names
fprintf(fid, 'stateFieldNames\t');
names = fieldnames(state);
for i = 1:length(names)
fprintf(fid, '%s\t', names{i});
end
fprintf(fid, '\r\n');
fprintf(fid, '#end\r\n\r\n\r\n');
end
%*************************************************************************
% 2. If this is the last call, close the output file
%*************************************************************************
if(type == -1)
fclose(fid);
rmfield(opt, 'outputfileFID');
return
end
%*************************************************************************
% 3. Output population to file
%*************************************************************************
fprintf(fid, '#Generation %d / %d\r\n', state.currentGen, opt.maxGen);
% output each state field
names = fieldnames(state);
for i = 1:length(names)
fprintf(fid, '%s\t%g\r\n', names{i}, getfield(state, names{i}));
end
fprintf(fid, '#end\r\n');
for i = 1:opt.numVar
fprintf(fid, 'Var%d\t', i);
end
for i = 1:opt.numObj
fprintf(fid, 'Obj%d\t', i);
end
for i = 1:opt.numCons
fprintf(fid, 'Cons%d\t', i);
end
fprintf(fid, '\r\n');
vertcat(result.pops(end,:).var);
for p = 1 : opt.popsize
for i = 1:opt.numVar
fprintf(fid, '%g\t', pop(p).var(i) );
end
for i = 1:opt.numObj
fprintf(fid, '%g\t', pop(p).obj(i) );
end
for i = 1:opt.numCons
fprintf(fid, '%g\t', pop(p).cons(i));
end
fprintf(fid, '\r\n');
end
fprintf(fid, '\r\n\r\n\r\n');
I am using this program for creating the output of MATLAB in text file. But I am not getting. I changed my code but unable to do so and getting result in graphical form which is not sufficient. Please help to get me out of this... Thanks

답변 (1개)

ES
ES 2017년 7월 27일

0 개 추천

Are you closing the file (by calling the function with type = -1)?

댓글 수: 1

Paridhi Rai
Paridhi Rai 2017년 7월 27일
No. I tried _ type poulations.txt_, but of no use

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2017년 7월 27일

댓글:

2017년 7월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by