Saving individual contents of struct into another folder other than the working directory

8 visualizaciones (últimos 30 días)
Hello,
I have a struct file 'x'. I wish to store each content of the file as an indiviual '.mat' file in a sub folder of the name 'best'. The code is as follows. I recieve an error message of the following. How can I modify my code?
%% Main aim of the script is to load only specific files from a list of names provided in
%% 'rFile' and then store these files in a subfolder called 'Best'
% Creating a cell consisting of names extracted fom 'rfile'
s = rFile.ImName;
% Adding an extention to each name in s
files = string(s) + '_avg_crd.mat';
n = numel(files);
% Creating the folder the files need to be saved
subfolder = fullfile(folder,'Best');
x = struct([]);
% the loop selects and stores in struct 'C.files_avg', only those files from the folder,
% that match the names with extension stored in 'files'.
% This list from C.files_avg is then saved with the same names into a subfolder of the original folder,
% called Best
for ii = 1:n
files_avg (ii) = fullfile(folder, files(ii));
C(ii).files_avg = load(files_avg{ii});
x {ii,2}= C(ii).files_avg;
x {ii,1}= files(ii);
matname = char;
matname = fullfile(subfolder, files(ii));
save(matname, x{ii,2}) ;
end
'Error using save
Must be a string scalar or character vector.'

Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de Mzo. de 2019
You can only save complete variables. You can assign to a variable and save that.
Remember that things saved need to appear as variables in mat files so it would not work to save a part of a variable.
  1 comentario
Walter Roberson
Walter Roberson el 28 de Mzo. de 2019
You might be interested in this point:
If you have a scalar struct, then you can use
save(matname, '-struct', 'NameOfStruct')
That will cause the fields of the variable NameOfStruct to be saved as individual variables in the file designated by matname .

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Search Path en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by