How can I save a variable of type cell or higher dimensional matrix with different filenames inside a parfor loop?

조회 수: 5 (최근 30일)
How can I save a variable of type cell or higher dimensional matrix with different filenames inside a parfor loop?. I use MATLAB version 2021. So I don't have acces to the new "formstruct" option which is availabe from MATLAB version 2024a. I defined a fuction parsave and called it inside the parfor loop to save my variables with different filenames. But it works only if the variable is a two dimensional matrix.
  댓글 수: 2
Damian Pietrus
Damian Pietrus 2024년 10월 16일
What's the error that you get when you try to save the file? Including a code snippet of the save function and where you call it in your parfor loop would be helpful.
madhurima bhattacharjee
madhurima bhattacharjee 2024년 10월 17일
Hi Damian,
Thanks a lot for your response. The issue resolved now. I was making the mistake of including the full path of the file in the argument of the parsave function and it kept giving the error that parsave cannot handle "char" type aruguments.

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

채택된 답변

Hitesh
Hitesh 2024년 10월 17일
Hi Madhurima,
You can save variables with different filenames by creating a separate function to handle the file-saving process when working with "parfor" loops. Since you are dealing with cell arrays or higher-dimensional matrices, you need to ensure that "parsave" function can handle these data types appropriately. Kindly refer to the below code for an example:
parfor i = 1:5
% Create a cell array or higher-dimensional matrix
myCellArray = {rand(3), rand(3)}
my3DMatrix = rand(3, 3, 3)
% Generate a filename for each iteration
cellFilename = sprintf('cellArray_%d.mat', i);
matrixFilename = sprintf('matrix3D_%d.mat', i);
% Save using the parsave function
parsave(cellFilename, myCellArray);
parsave(matrixFilename, my3DMatrix);
end
function parsave(filename, var)
save(filename, 'var');
end
If the issue persists, could you please share the code file where you are encountering any error? So that I can investigate the issue.
For more information regarding, refer to this MATLAB documentation:
  댓글 수: 1
madhurima bhattacharjee
madhurima bhattacharjee 2024년 10월 17일
Hi Hitesh,
Thanks a lot for the code. It works. I was defining the parsave function just like you did but when calling the function I was including the the full path of the file including the directory in the argument of the parsave function instead of assigning the filename in a separate variable before calling parsave. That's why it kept giving error that parsave cannot handle "char" type arguments. Again thanks a lot.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by