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

4 Ansichten (letzte 30 Tage)
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 Kommentare
Damian Pietrus
Damian Pietrus am 16 Okt. 2024
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 am 17 Okt. 2024
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Hitesh
Hitesh am 17 Okt. 2024
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 Kommentar
madhurima bhattacharjee
madhurima bhattacharjee am 17 Okt. 2024
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by