For loop to plot numbered double arrays

1 回表示 (過去 30 日間)
Felix Schuermann
Felix Schuermann 2018 年 3 月 17 日
コメント済み: Stephen23 2018 年 3 月 20 日
I loaded a file wich contains a lot of double arrays. The arrays are named like this array1, array2, array3 and so on. The arrays contain int values wich I'd like to plot (first column vs. second column). How do you do that with a for loop?
  1 件のコメント
Stephen23
Stephen23 2018 年 3 月 20 日
"The arrays are named like this array1, array2, array3 and so on. .... How do you do that with a for loop?"
The best solution to this problem is to avoid this situation entirely. Dynamically accessing variable names is one way that beginners force themselves into writing slow, complex, buggy, hard to debug code. Luckily it is also trivially easy to avoid this situation, by simply loading the data into one variable. Venkata Siva Krishna Madala's answer shows you how simple this is, without magically accessing variable names.
If you want to learn why dynamically accessing variable names is a bad way to write code then read this page and all of its linked pages:

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

回答 (1 件)

Venkata Siva Krishna Madala
Venkata Siva Krishna Madala 2018 年 3 月 20 日
Hello Felix,
Since you have not given a sample file I assume the data to be in a MAT file called "sample.mat". It can contain any number of n x 2 arrays.
s=load('sample.mat');
s=struct2cell(s);
for i=1:size(s,1)
figure(i)
plot(s{i}(:,2),s{i}(:,1));
end
You can similary load data using xlsread function for Excel files and readtabel for text files and so on. For further information please visit https://www.mathworks.com/help/matlab/standard-file-formats.html
Regards,
Krishna Madala

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by