How to concentrate matrix 100 times?

조회 수: 2 (최근 30일)
Tsuwei Tan
Tsuwei Tan 2019년 2월 11일
편집: Stephen23 2019년 2월 11일
for testnum=1:100
filesavepath=['H:\Desktop\0208\' num2str(testnum) '_sen'];
load(fullfile([filesavepath '\0208_' num2str(testnum) '_sen_avg_3.mat']),'matrix_avg');
eval(sprintf('matrix_%g=matrix_avg;',testnum));
end
matrix=cat(2,matrix_1,matrix_2,matrix_3,matrix_4,matrix_5,matrix_6,matrix_7,matrix_8,matrix_9,matrix_10,matrix_11,matrix_12);
I am trying to concentrate 100 matrix for instance. I know how to load them 100 times at different folder, but how to make my last line
matrix=cat(2,matrix_1,matrix_2,.....matrix_99,matrix_100);
Thank you!
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2019년 2월 11일
Tsuwei - are all of your matrices of the same dimension? If not, then you could use a cell array to store all of these matrices.
myMatrices = cell(100,1);
for testnum=1:100
filesavepath=['H:\Desktop\0208\' num2str(testnum) '_sen'];
load(fullfile([filesavepath '\0208_' num2str(testnum) '_sen_avg_3.mat']),'matrix_avg');
myMatrices{testNum} = matrix_avg;
end
If they are all of the same dimension then you should be able to create a single matrix that is sized appropriately.
In either case, try to avoid dynamically creating matrices with eval. This can lead to errors and has been discussed elsewhere in this forum.

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

채택된 답변

Stephen23
Stephen23 2019년 2월 11일
편집: Stephen23 2019년 2월 11일
Your approach is entirely in the wrong direction: using numbered variables is a sign that you are doing something wrong with your data/code design. Read this to know some of the reasons why:
The simpler alternative is to load the file data into an output variable (which is a structure), then your task is trivially easy (and much more efficient):
N = 100;
C = cell(1,N);
for k = 1:N
F = fullfile(...);
S = load(F,'matrix_avg');
C{k} = S.matrix_avg;
end
M = cat(2,C{:})
See also:

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by