필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Jonathan Demmer
Jonathan Demmer 2019년 3월 19일
댓글: Jonathan Demmer 2019년 3월 21일
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
Stephen23
Stephen23 2019년 3월 19일
"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 2019년 3월 19일
Sorry my mistake its not matrices, its different files containing matrices.
Regards

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

답변 (1개)

Guillaume
Guillaume 2019년 3월 19일
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
Guillaume
Guillaume 2019년 3월 19일
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 2019년 3월 21일
Thank you very much for your answer its really helpful!!
Jonathan

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

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by