How can I rearrange the order of N matrix in Matlab?

1 view (last 30 days)
Hello all,
I created N matrix on matlab. named:
matrix_001
matrix_002
matrix_003
...
matrixN-1; matrix_N.
I would like to write a code which reorder the matrix that the last one become the first etc...
So i will look like:
matrix_N becomes matrix_001
matrix_N-1 becomes matrix_002
...
matrix_002 becomes matrix_N-1
Matrix_001 becomes matrix_N
Can someone help me to do that please?
  3 Comments
Stephen23
Stephen23 on 19 Mar 2019
"I created N matrix on matlab. named:"
matrix_001
matrix_002
matrix_003
Numbered variables are a sign that you are doing something wrong. Bad data design (like numbering variables) forces beginners to write bad code. Do NOT do this.
Dynamically accessing variable names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
Indexing is simple, neat, easy to debug, and very efficient (unlike what you are trying to do). You should use indexing, exactly as the MATLAB documentation recommends: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
Jonathan Demmer
Jonathan Demmer on 19 Mar 2019
Sorry my mistake its not matrices, its different files containing matrices.
Regards

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 19 Mar 2019
Read Stephen's comment and follow the links to understand why your original design is very flawed.
You want only one variable containing all the matrices. It can either be a cell array or an N-d matrix (where N is 1 dimension more than your current matrices). Either way, rearranging the order of the matrices becomes a one-liner:
%with a cell array
%c = {matrix_001, matrix_002, etc...};
newc = c(end:-1:1);
%with an N-d array
%m = cat(ndims(matrix_001)+1, matrix_001, matrix_002, ...);
newm = flip(m, ndims(m));
  6 Comments
Guillaume
Guillaume on 19 Mar 2019
This should do it. Note that is assumes that your file follow exactly the pattern you've detailed as I'm relying on the fact that with that pattern the alphabetic order of the file is the same as the numeric one.
With some more complex code, we could actually extract the numeric part of the file, I'm not doing that here
folder = 'C:\somewhere\some\folder';
tempname = '_temprename'; %can't rename in place, so move files in temp folder
tempfolder = fullfile(folder, tempname);
if ~exist(tempfolder, 'dir')
mkdir(tempfolder);
else
error('temporary renaming folder already exist. Aborting to avoid overwriting files');
end
filelist = dir(fullfile(folder, 'VELOCITY_*.mat')); %get file list, orderered alphabetically
sourcefiles = {filelist.name};
destfiles = sourcefiles(end:-1:1); %reverse order of files
cellfun(@movefile, fullfile(folder, sourcefiles), fullfile(tempfolder, destfiles)); %move and rename files in temp folder
movefile(fullfile(tempfolder, '*.*'), folder) %move renamed file back into original folder
rmdir(tempfolder); %delete tempfolder
Note that the code is untested. Test it on a copy of your folder first, to make sure it works as desired and you don't loose any data.
Jonathan Demmer
Jonathan Demmer on 21 Mar 2019
Thank you very much for your answer its really helpful!!
Jonathan

Sign in to comment.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!