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

8 次查看(过去 30 天)
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