Read in the the (i,j) values of N number of matrices

2 ビュー (過去 30 日間)
Michael Whipple
Michael Whipple 2019 年 2 月 24 日
編集済み: Stephen23 2019 年 2 月 25 日
I want to read in the (i,j) values of a variable number of matrices.
Q1 = xlsread(FILENAME,1,'A16:C16');
Q2 = xlsread(FILENAME,2,'A16:C16');
Q3 = xlsread(FILENAME,3,'A16:C16');
Q4 = xlsread(FILENAME,4,'A16:C16');
Q5 = xlsread(FILENAME,5,'A16:C16');
for i=1:3
for j=1:3
for k=1:N
Qlam(1,k) = Q(i,j);
end
end
end
I have read in the matrices I want now I need to do calculations for the (i,j) values of each matrix. I want to store them in Qlam to access later in the second for loop. Is there a way to do this with a for loop?
  2 件のコメント
Stephen23
Stephen23 2019 年 2 月 24 日
Numbering variables is a sign that you are doing something wrong. Repeating basically the same code (i.e. copy and pasting) is a sign that you are doing something wrong.
Trying to access variable names dynamically is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
Michael Whipple
Michael Whipple 2019 年 2 月 24 日
Then what's a better to do that? I need a "Q" matrix for each layer of a laminate and thats what is being read in at the beginning. Is there a better way to store them?

サインインしてコメントする。

回答 (1 件)

Stephen23
Stephen23 2019 年 2 月 24 日
編集済み: Stephen23 2019 年 2 月 24 日
Just use a cell array, then you can trivially access the matrices using indexing:
For example:
N = 5;
Q = cell(1,N);
for k = 1:N
Q{k} = xlsread(FILENAME,k,'A16:C16');
end
  2 件のコメント
Michael Whipple
Michael Whipple 2019 年 2 月 24 日
Is there a way to use the Cell with a double? I get an error the doubles can not be converted to a cell.
Stephen23
Stephen23 2019 年 2 月 25 日
編集済み: Stephen23 2019 年 2 月 25 日
"I get an error the doubles can not be converted to a cell."
Nothing in my answer would obviously cause such an error, so it is likely to be something that you did. But as you did not actually show the code that you used, I will have to rely on my magical crystal ball to look at your computer monitor and see what you tried. Unfotunately my magical crystal ball is at the workshop for repairs, and I might not get it back for a few weeks. In the meantime, you can help by actually showing the code that you tried.
Thank you for your understanding.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by