How can I link a matrix to a menu selections.

조회 수: 2 (최근 30일)
Ben Hischar
Ben Hischar 2021년 8월 4일
댓글: Chunru 2021년 8월 5일
Hi all,
I am currently very inexperienced with Matlab and the frustration is slightly ensuing.
I have a situation where I need to link two menu options that are selected two to different 4x5 matricies.
For example I currently have distances in miles and kilometers in two 4x5 matricies.
I have started the code with;
x = menu('Where from?' , 'A' , 'B' , 'C', 'D');
y = menu('Where to?' , 'E' , 'F' , 'G' , 'H' , 'I');
to get my menu options.
I then have my two matrcies.
kilometers = [2887 7277 11628 7171 11856
5132 4432 10360 5450 12193
2624 7366 10321 8203 13201
5344 5333 8308 7394 14816];
miles = [1235 3930 6279 3892 6402
2771 2393 5740 2943 5584
1417 3977 5573 4429 7128
2886 2880 4486 4285 8001];
I am trying to link the menu so that when the promt comes up and says 'where from?' and 'A' is sleceted, then the second promt 'where to? and 'I' is selected. It will display 'The distance from 'A' to 'I' is 14816 kilometers or 8001 miles'.
Thank you for your help in advance!
Ben
  댓글 수: 2
Paul Kaufmann
Paul Kaufmann 2021년 8월 4일
It's not quite obvious, why the result of picking "A" and "I" should be 14816 or 8001. The two matrices make more sense in the scenario where the last entry (i.e. 14816) belongs to the selection of "D" and "I". Or am I missing something?
The answer by Chunru seems to go in that direction as well.
Chunru
Chunru 2021년 8월 5일
The two matrices have rows corresponding to cities 'A'..'D', and columns corresponding to cities 'E'...'I'. That's is my guess.

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

채택된 답변

Chunru
Chunru 2021년 8월 4일
kilometers = [2887 7277 11628 7171 11856
5132 4432 10360 5450 12193
2624 7366 10321 8203 13201
5344 5333 8308 7394 14816];
miles = [1235 3930 6279 3892 6402
2771 2393 5740 2943 5584
1417 3977 5573 4429 7128
2886 2880 4486 4285 8001];
x = menu('Where from?' , 'A' , 'B' , 'C', 'D');
y = menu('Where to?' , 'E' , 'F' , 'G' , 'H' , 'I');
dist_km = kilometers(x, y);
dist_miles = miles(x, y);
fprintf('Distance from %d to %d is %.0fkm or %.0fmiles\n', 'A'+x-1, 'E'+y-1, dist_km, dist_miles)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by