Getting error in PARFOR in MATLAB

I was using PARFOR to create 80 files using MATLAB. The code is as below:
function [t,ele_num_pipe,support_nodes] = write_kfile_pipe_final2( P, inundation_depth, D , A, L, sigy , flow_direction, sim_num )
However, I am getting an error message in the follwoing line of write_kfile_pipe_final2.m function.
fileID = fopen('shell_',num2str(sim_num),'.txt','w');
Error: "INVALID PERMISSION"
I found some tips to solve this in mathworks (Invalid permission error with fopen()). The invalid permission error was resolved by modifying the line as below:
name = 'shell_',num2str(sim_num),'.txt';
fileID = fopen(name,'w');
However, I am now getting another error message in the following line:
shell_ele = load('shell_',num2str(sim_num),'.txt');
Error message:
"File shell_ is in use by another process or thread. You should be able to load data once the other process or thread has released the file."
Can anyone help me in this, please?

1 Comment

"Can anyone help me in this, please?"
Both of the errors have exactly the same cause. The FOPEN documentation states that its 1st argument must be a filename, and its optional 2nd argument the permission. Lets take a look at how you called it:
fileID = fopen('shell_',num2str(sim_num),'.txt','w');
% ^^^^^^^^ 1st: part of filename
% ^^^^^^^^^^^^^^^^ 2nd: part of filename
% ^^^^^^ 3rd: part of filename
% ^^^ 4th: permission
You then did exactly the same thing again when you called LOAD. The LOAD documentation states that its 1st argument must be the filename. This is how you called it:
shell_ele = load('shell_',num2str(sim_num),'.txt');
% ^^^^^^^^ 1st: part of filename
% ^^^^^^^^^^^^^^^^ 2nd: part of filename
% ^^^^^^ 3rd: part of filename
You need to follow the syntaxes given in the documentation: if the documentation states that the filename is one input then you need to provide it as one input, not as three inputs.

Sign in to comment.

Answers (1)

Matt J
Matt J on 4 Sep 2023
It looks like you are trying to read a file (with load()) while it's being written to. Perhaps you mean to do the reading first? Or perhaps you have finished writing to the file and need to close it? If the latter, see fclose.

8 Comments

Thanks for the reply. I was actually writing like this:
name = 'shell_',num2str(sim_num),'.txt';
fileID = fopen(name,'w');
len =0;
ele_loc = [];
......................................(codes)
end
fclose(fileID);
Second time,
name = 'shell_',num2str(sim_num),'.txt';
fileID = fopen(name,'a');
for i=1:size(elbowx1,1)
...........(codes)
end
fclose(fileID);
This operation continued 5 times and finally I was trying to load it like this:
fprintf(fileID,'*PART\n');
fprintf(fileID,'$# title\n');
fprintf(fileID,'pipe%i\n',i);
.......................
....................
fprintf(fileID,'*ELEMENT_SHELL\n');
fprintf(fileID,'$# eid pid n1 n2 n3 n4 n5 n6 n7 n8\n');
shell_ele = load('shell_',num2str(sim_num),'.txt'); %%%%% "HERE I GOT THE ERROR MESSAGE"
fprintf(fileID,'%8i%8i%8i%8i%8i%8i%8i%8i%8i%8i\n',shell_ele');
I would have expected as follows,
name = 'shell_',num2str(sim_num),'.txt';
shell_ele = load(name);
fileID = fopen(name,'a');
for i=1:size(elbowx1,1)
...........(codes)
end
fprintf(fileID,'*PART\n');
fprintf(fileID,'$# title\n');
fprintf(fileID,'pipe%i\n',i);
...........................................
fprintf(fileID,'*ELEMENT_SHELL\n');
fprintf(fileID,'$# eid pid n1 n2 n3 n4 n5 n6 n7 n8\n');
fprintf(fileID,'%8i%8i%8i%8i%8i%8i%8i%8i%8i%8i\n',shell_ele');
fclose(fileID);
Thanks Matt J. However, still getting the same error message "File shell_ is in use by another process or thread. You should be able to load data once the other process or thread has released the file." Very frustrating.
I have attahced my codes and below are lines where name = 'shell_',num2str(sim_num),'.txt'; has been used. I would really appreciate if you could help me as I am stuck with this for a week.
Lines: 64,147,233,327,410,530,833,839 are the lines in write_kfile_pipe_final3.
Thanks again.
The files don't appear to show how you implemented my suggestions. Therefore, I am unable to guess why it didn't work.
I can't say this will resolve the issue, but lines 64, 147, 233, 327, & 410 aren't doing what you think they are
name = 'shell_',num2str(sim_num),'.txt';
should be
name = ['shell_',num2str(sim_num),'.txt'];
You need to concatenate the strings. Fix those and report back.
These days I would suggest
name = "shel_" + sim_num + ".txt";
Thank you guys. It worked now. I did not understand this could give me error and was busy looking to find error everywhere except it :)
Thanks again.
fileID = fopen('shell_',num2str(sim_num),'.txt','w');
means that the character vector 'shell_' should be passed as the first parameter; that the numeric value sim_num should be converted to character representation of decimal and passed as the second parameter; that the character vector '.txt' should be passed as the third parameter, and the character vector 'w' should be passed as the 4th parameter
name = 'shell_',num2str(sim_num),'.txt';
That means that the character vector 'shell_' should be assigned to the variable name and then the result should be displayed like
name =
'shell'
After that, the numeric value sim_num should be converted to character representation of decimal, and then the result should be displayed like
ans =
'2'
and then the character vector '.txt' should be stored in the internal variable named ans and nothing should be displayed.
If you want to combine character vectors into a single character vector, you need to use [] or strcat() -- or use string objects with the + method of string() objects

Sign in to comment.

Products

Tags

Asked:

on 4 Sep 2023

Edited:

on 5 Sep 2023

Community Treasure Hunt

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

Start Hunting!