How to get 3D matrix when using int2string for two variables
8 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 21 Jul 2016
Edited: Stephen23
on 21 Jul 2016
"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:
Accepted Answer
Azzi Abdelmalek
on 21 Jul 2016
Edited: Azzi Abdelmalek
on 21 Jul 2016
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 Comments
More Answers (0)
See Also
Categories
Find more on Variables 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!