Creating multiple plots from a data file using a for loop

20 ビュー (過去 30 日間)
Anne Reuten
Anne Reuten 2019 年 7 月 10 日
回答済み: Shubham Sangle 2019 年 7 月 10 日
Hi all,
I am very new to Matlab, and I am trying to plot multiple figures from a dataset using a for loop.
However, it's not working and I don't how to solve it.
The idea is that the loop plots 42 different figures (one for each participant), and that the first figure contains the data from the 1:14 row from columns 3 (x-axis) and column 5 (reaction times), figure 2 should contain the data from the 15:28 row from columns 3 (x-axis) and column 5 (y-axis), etc..
Below is the code that I have used:
Thai_UniversalPictures_Categorization = readtable('Thai_UniversalPictures_Categorization.xlsx'); % This is the table with the data
figure;
hold on;
for i = 1 : 14 : 588(Thai_UniversalPictures_Categorization); % I would like 42 figures
x = Thai_UniversalPictures_Categorization(i:1+13, 3); % The x-axis should contain the picture's names, which are located in the third column of the data file. The i:1+13 was written as an attempt to tell the code it's a loop and that it should take the first 14 pictures' names for the first figure, the next 14 pictures for the second figure etc.
y = Thai_UniversalPictures_Categorization(1:1+13, 5); % The y-axis should contain reaction times , which are located in the fifth column of the data file. The i:1+13 was written as an attempt to tell the code it's a loop and that it should take the first 14 reaction times for the first figure, the next 14 reaction times for the second figure etc.
plot(x,y);
end
I get an error which says:
Error using tabular/plot
Too many input arguments.
Error in outlierplots (line 11)
plot(x,y);
I hope you can follow my explanation and help me.
Best regards, Anne
  6 件のコメント
Anne Reuten
Anne Reuten 2019 年 7 月 10 日
編集済み: Anne Reuten 2019 年 7 月 10 日
I have adapted the code to this:
Thai_UniversalPictures_Categorization = readtable('Thai_UniversalPictures_Categorization.xlsx');
%% Reaction time
for i = 1 : 14 : 588(Thai_UniversalPictures_Categorization);
figure;
hold on;
plot(Thai_UniversalPictures_Categorization{i:1+13, 5}); title('Reactiontime');
end
It is now creating 42 figures, but only the first one contains data (from the first 14 rows) and the others are empty.
Anne Reuten
Anne Reuten 2019 年 7 月 10 日
I think I managed to get it working correctly.
In case someone else is trying to do a similar thing and is struggling, I used this code:
Thai_UniversalPictures_Categorization = readtable('Thai_UniversalPictures_Categorization.xlsx');
%% Reaction time
for i = 1 : 14 : 588(Thai_UniversalPictures_Categorization);
figure;
hold on;
plot(Thai_UniversalPictures_Categorization{i:i+13, 5}); title('Reactiontime');
end

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

採用された回答

Guillaume
Guillaume 2019 年 7 月 10 日
I see you've solved your problem, but for future reference the problem had nothing to do with plot. The problem was with your incorrect use of indexing.
() indexing on a table returns a portion of a table as a table
{} indexing on a table returns a portion of the content of the table as whatever type that content is.
Alternatively, you can use . indexing to get a particular variable of the table, so
plot(sometable(:, somecolumn), sometable(:, someothercolumn))
is never going to work as you are passing tables to plot
plot(sometable{:, somecolumn}, sometable{:, someothercolumn})
or
plot(sometable.nameofsomecolumn, sometable.nameofsomeothercolumn)
is the correct syntax.
  1 件のコメント
Anne Reuten
Anne Reuten 2019 年 7 月 10 日
Thank you very much for your explanation!

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

その他の回答 (2 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 10 日
plot(x',y');

Shubham Sangle
Shubham Sangle 2019 年 7 月 10 日
You can use this,
() indexing on a table returns a portion of a table as a table
plot(table(:, column1), table(:, column2))

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by