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

조회 수: 1 (최근 30일)
Gyp
Gyp 2019년 12월 21일
편집: Stephen23 2019년 12월 21일
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