How to get 3D matrix when using int2string for two variables

조회 수: 3 (최근 30일)
Sophi gra
Sophi gra 2016년 7월 21일
편집: Stephen23 2016년 7월 21일
Hi everyone!I have a problem to gather my data in a specific matrix! I have 200 matrices 20 by 50. I have to make a matrix 20 by 50 by 200. the problem is when I use eval and int2string command I got 4D matrix. because my data are like W1T1,W1T2,..W1T20,W2T1..,W2T20,..,W10T1,..,W10T20. and here is my code:
for i = 1:10
for j = 1:20
eval([(:,:,' int2str(i) ',' int2str(j) ') = W' int2str(i) 'T' int2str(j) ';'];
end
end
then I got a matrix (:,:,i,j) with the dimension of 20 by 50 by 10 by 20! is there any way to get a 3D matrix 20 by 50 by 200?
  댓글 수: 1
Stephen23
Stephen23 2016년 7월 21일
편집: Stephen23 2016년 7월 21일
"is there any way to get a 3D matrix 20 by 50 by 200?"
Yes, and it would be lot simpler when beginners learn to avoid using eval for such trivial code. In fact you already asked this question, and got a very good answer that used robust load-ing into a variable (and not the buggy eval method):
Instead of that fast and robust method you decided to use a slow and buggy eval, and what a surprise! It is buggy! What your are doing inside that eval string makes no sense, it not even valid MATLAB syntax. The MATLAB editor would have pointed this out to you if you were not using the slow and buggy eval method...
Using eval for this task is a bad way to permform a trivial task like this, and a bad habit to unlearn later:

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 21일
편집: Azzi Abdelmalek 2016년 7월 21일
You don't need to use Eval function:
W1T1=rand(4)
W1T2=rand(4)
W2T1=rand(4)
W2T2=rand(4)
s=whos('-regexp' ,'^W\dT\d$')
n=numel(s)
name=s(1).name
save('file.mat',name,'-append')
for k=2:n
name=s(k).name
save('file.mat',name,'-append')
end
d=load('file')
nam=fieldnames(d)
for k=1:n
M(:,:,k)=d.(nam{k})
end

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by