Creating a table for the "For-loop command" results:

1 次查看(过去 30 天)
Hi,
I am working on a task. I have to use the "for-loop" command. I already used it, now I would like to create a table with the results. I would like to see in the table the option of the menu that was selected and the number of seeds that are allowed given the area that the person inserted. But I don't know how to make that table with the well established results. I don't know a command for it.
clear
clc
n=input('Enter the number of time you want this program to run.n= ');
Area=input('Enter the area of the land in m2. Area= ');
D = sqrt(Area*10000); %This will change the area from m2 to cm.
for i=1:n
v= menu('Select the aromatic species with which you will work:', 'Basil' ,'Cilantro', 'Dill', 'Tarragon', 'Spearmint', 'Oregano', 'Parsley', 'Thyme');
if v==1
distrows =15;
distplant = 30;
elseif v==2
distrows= 15;
distplant =20;
elseif v==3
distrows =15;
distplant = 20;
elseif v==4
distrows =15;
distplant = 20;
elseif v==5
distrows =25;
distplant = 50;
elseif v==6
distrows =15;
distplant=20;
elseif v==7
distrows =15;
distplant = 20;
elseif v==8
distrows =15;
distplant = 20;
end
rows=D/distrows;
seedrow=rows/distplant;
seeds=round(rows *seedrow)
end
  1 个评论
Stephen23
Stephen23 2019-12-21
编辑:Stephen23 2019-12-21
Note that a "table" is a particular data type in MATLAB, most likely you are referring to a "numeric array" or a "numeric matrix".
"I don't know a command for it."
You will need to use indexing to allocate the output to an array. How to do that is shown in the introductory tutorials:
and I would also recommend reading this:
Also note that you can replace all of that if - else construct with a few vectors and some basic indexing, it would take just a few lines of code.
Use of indexing is one of MATLAB's main features, and if you want to learn how to use MATLAB then you need to learn to think in terms of vectors/matrices/arrays and their indices.

请先登录,再进行评论。

回答(1 个)

Ridwan Alam
Ridwan Alam 2019-12-21
编辑:Ridwan Alam 2019-12-21
Even though there are many more efficient ways to do this, this is a simple addition to your code:
clear
clc
n=input('Enter the number of time you want this program to run.n= ');
Area=input('Enter the area of the land in m2. Area= ');
D = sqrt(Area*10000); %This will change the area from m2 to cm.
myTable = zeros(n,2);
for i=1:n
v= menu('Select the aromatic species with which you will work:', 'Basil' ,'Cilantro', 'Dill', 'Tarragon', 'Spearmint', 'Oregano', 'Parsley', 'Thyme');
if v==1
distrows =15;
distplant = 30;
elseif v==2
distrows= 15;
distplant =20;
elseif v==3
distrows =15;
distplant = 20;
elseif v==4
distrows =15;
distplant = 20;
elseif v==5
distrows =25;
distplant = 50;
elseif v==6
distrows =15;
distplant=20;
elseif v==7
distrows =15;
distplant = 20;
elseif v==8
distrows =15;
distplant = 20;
end
rows=D/distrows;
seedrow=rows/distplant;
seeds=round(rows *seedrow)
myTable(n,:) = [v,seeds];
end
% if you really prefer it as a "table"
myTable = array2table(myTable);
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by