Getting error in PARFOR in MATLAB

1 view (last 30 days)
Md Mia
Md Mia on 4 Sep 2023
Edited: Stephen23 on 5 Sep 2023
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
Stephen23
Stephen23 on 5 Sep 2023
Edited: Stephen23 on 5 Sep 2023
"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
Md Mia
Md Mia on 5 Sep 2023
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.
Walter Roberson
Walter Roberson on 5 Sep 2023
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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!