Error writing to a file

6 views (last 30 days)
Shreenath Krishnamurthy
Shreenath Krishnamurthy on 11 Mar 2023
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..

Answers (1)

Ganapathi Subramanian
Ganapathi Subramanian on 27 Mar 2023
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 Comments
Ganapathi Subramanian
Ganapathi Subramanian on 27 Mar 2023
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
Shreenath Krishnamurthy on 3 Apr 2023
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.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!