Error writing to a file

5 次查看(过去 30 天)
I have a gamainfile
options = optimoptions('gamultiobj','UseParallel', true,'UseVectorized', false,'ParetoFraction',0.35,...
'PopulationSize',60,'Generations',60, 'CrossoverFraction',0.5,'CrossoverFcn',@crossoverintermediate ,'MutationFcn',...
@mutationadaptfeasible)
[X,fval,exitflag,output,final_pop]=gamultiobj(@functionfile,NumVar,A,b,[],[],...
Xlow,Xup,options);
The function file has
Input parameters X(1)…X(6), constants
[time1,Y1]=ode15s(odefn1…..)
[time2,Y2]=ode15s(odefn1…..)
Y1 and Y2 have dimensions (n x 270)
n is the number of rows based on the time step
Calc1 to Calc4 are calculated by integrating various Y values in Y1 and Y2.
calc1= f1(Y1,Y2);
calc2= f2(Y1,Y2);
calc3= f3(Y1,Y2);
calc4= f1(Y1,Y3);
The objectives are
Obj1 =Calc3/100+ 10000*(max(0,constraint1-calc1/100))^2+10000*(max(0,constraint2-calc3/100))^2;
Obj2 =1/calc4+ 10000*(max(0,constraint1-calc1/100))^2+10000*(max(0,constraint2-calc3/100))^2;
filename = [tempname('directory name),'.txt'];
fid = fopen(filename,'w');
fprintf(fid, '%f %f %f %f %f %f %f %f %f %f\n',X(1),X(2),X(3),X(4),X(5),X(6),calc1,calc2,calc3,calc4);
fclose(fid);
I get an error message
Invalid fileID for worker. File operations must occur on the worker where the file was opened
How do I avoid this error and is there a better way to save all the values that I write to the files..

回答(1 个)

Ganapathi Subramanian
It is my understanding that you are working on a program to store its data inside a file and have trouble in writing the data to the file. Refer to the below code snippet for writing the data to a unique temporary file.
filename=[tempname('C:/foldername'),'.txt'];
fid=fopen(filename,'w');
i=5.6;
fprintf(fid,'%f',i);
For more information regarding ‘tempname’, refer this link
Refer to the below documentation for more information regarding ‘fprintf.
  3 个评论
Ganapathi Subramanian
Hi Shreenath Krishnamurthy,can you please share more information regarding 'if multiple files access the same work it should not be a problem right' .It is not clear.What does 'multiple files' depict? Is it the set of programs?
Shreenath Krishnamurthy
Multiple files depict multiple text files. These files have temporary names and write outputs to the input conditions to the ga file. The gamultiobj uses the parallel evaluation option.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by