MATLAB Answers

how to choose the value?

조회 수: 2(최근 30일)
win
win 14 May 2013
i have some data. see below if true
l1 = input('bus connection(choose one number from 1 to 30) = ');
bus1 = [2 3];
bus2 = [1 4 6 5];
bus3 = [1 4];
bus4 = [2 3 6 12];
bus5 = [2 7];
bus6 = [2 4 7 8 9 10 28];
bus7 = [5 6];
bus8 = [6 28];
bus9 = [6 10 11];
bus10 = [6 9 17 20 21 22];
bus11 = 9;
bus12 = [4 13 14 15 16];
bus13 = 12;
bus14 = [12 15];
bus15 = [12 14 18 23];
bus16 = [12 17];
bus17 = [16 10];
bus18 = [15 19];
bus19 = [18 20];
bus20 = [10 19];
bus21 = [10 22];
bus22 = [10 21 24];
bus23 = [15 24];
bus24 = [22 23 25];
bus25 = [24 26 27];
bus26 = 25;
bus27 = [25 28 29 30];
bus28 = [6 8 27];
bus29 = [27 30];
bus30 = [29 27];
end
when i press 1 it display bus1 matrix also when i press 2 it show bus2 matrix and bus3,bus4 etc., please anyone help me

  댓글 수: 2

Thomas
Thomas 14 May 2013
Goodwin, your code will not work as it is. I assume you want the user to input the number and then you want the corresponding bus matrix to show up..
Where do you compare what the user has entered to what you want to look up? you could do this by a number of 'if' statements (just think about it.. ) or use switch case..
win
win 14 May 2013
thanks bro.........

댓글을 달려면 로그인하십시오.

채택된 답변

Thomas
Thomas 14 May 2013
An example of how to use switch case: adapt for your particular case
busNum=input('Enter the bus number:');
switch busNum
case 1
disp('2 3')
case 2
disp('1 4 6 5')
end

  댓글 수: 0

댓글을 달려면 로그인하십시오.

추가 답변(2개)

Jan
Jan 14 May 2013
Storing the values in different variables is a bad idea. Better use an array:
bus = {[2 3], ...
[1 4 6 5], ...
[1 4], ...
etc. };
l1 = input('bus connection(choose one number from 1 to 30) = ');
disp(bus{l1});

  댓글 수: 1

win
win 14 May 2013
thanks bro...... i learned something here. thanks for all of you

댓글을 달려면 로그인하십시오.


Jurgen
Jurgen 14 May 2013
I suggest you concatenate the numerical input l1 with the string 'bus'. Then display the content with display(). E.g.
X = ['bus' l1];
display(X)

  댓글 수: 5

표시 이전 댓글 수: 2
Jan
Jan 14 May 2013
@godwin tgn: No, it is your turn to rewrite the code.
Do not use EVAL. It results in more troubles than it solves.
Jurgen
Jurgen 15 May 2013
Well it should work for this simple case?
José-Luis
José-Luis 15 May 2013
It is not a matter of it working or not. Please look at this previous discussion.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by