MATLAB Answers

Rename and save multiple .mat files.

26 views (last 30 days)
caesar on 13 Oct 2019
Commented: caesar on 15 Oct 2019
I am trying to load, rename and save a group of mat files . so far I am using this code :
cd 'E:\Mat'
fileFolder = fullfile('E:\','Mat');
% cd (fileFolder)
dirOutput = dir(fullfile(fileFolder,'*.mat'));
fileNames = {};
for k=1:length(fileNames)
movefile(H, sprintf('All_state_action_qs_%03d.mat', k));
although it does rename all the files properly, when I load any files (with new name) the name on the work space is the old name.
Therfore, I guess there is a way for doing Load>Rename>save again in a batch fashion.


Show 1 older comment
caesar on 15 Oct 2019
yes , only one variable that is an array either :
A- single row vector of 24 elements ( numbers). for instance the Array_1.mat has only a 1*24 array named Array_1.
B- N By 8 array of elements ( number).
Because an error has happened, I want to rename the mat files and the single vector in each one altogether. assume there is three mat files :
Array_1.mat, Array_2.mat and Array_3.mat where they have Array_1, Array_2 and Array_3 respctively. I want to rename them as
A_12.mat,A_13.mat and A_14.mat,with vectors A_12,A_13 and A_14 respectively.
also I would be grateful if you can tell me how can extract the vector from within those mat files and stack them in one N*24 matrix . [A_12;A_13;A_14].
Stephen Cobeldick
Stephen Cobeldick on 15 Oct 2019
@caesar: putting numbers into variable names is a sign that you are doing something wrong. It is unlikely to help you, just make your code complex, slow, obfuscated, and buggy.
"also I would be grateful if you can tell me how can extract the vector from within those mat files and stack them in one N*24 matrix"
I get the feeling that you are not actually asking us what you are really trying to achieve:
Probably it would also help if you uploaded some sample data, by clicking the paperclip button.
caesar on 15 Oct 2019
Ok , sorry if did confuse you . assume I have the following code
first_1=[1 1 1 1 1 1];
first_2=[2 2 2 2 2 2];
first_3=[3 3 3 3 3 3];
and I want to :
1- load first_1.mat,firs_2.mat and firs_3.mat
3- rename vectors first_1,first_2 and first_3 to:
3- save them in mat files :
firs_11.mat,firs_12.mat and firs_13.mat
3 - Form ALL that looks like :
all =
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
I hope this time its clearer

Sign in to comment.

Answers (1)

per isakson
per isakson on 14 Oct 2019
"the name on the work space is the old name" What do you expect? It's the names of the variables contained in the mat-file. They aren't affected by the name of the mat-file.
How exactly do you load the renamed mat-file?


Sign in to comment.

Translated by