How to get menu to call code sections
3 ビュー (過去 30 日間)
古いコメントを表示
Aleksander Vae Haaland
2019 年 9 月 19 日
回答済み: Aleksander Vae Haaland
2019 年 9 月 19 日
I am working on a assignment where I am manipulation an image. All the code is written in code sections corresponding to the task number.
The last task on the assignment is to add a menu which runs the code for the different task.
For example: Task 2: Show image. Code: imshow(A).
Is there a way I can call the different code sections with my menu, can I create functions and call them, or do I need to copy all my code into the switch case section of my menu? The menu is the last task, therefore it is currently on the bottom of my code.
%% Task 1: Load the image data
A = imread('butterfly.jpg');
%% Task 2: Show image
function taskTwo
imshow(A);
end
%% Task 10: Menu
choice = 1;
while choice ~= 7
choice = menu('Toolbar', 'Task 2', 'Task 5', 'Task 6', 'Task 7', 'Task 8', 'Task 9', 'Exit');
switch choice
case 1
taskTwo
case 7
close all
end
end
1 件のコメント
Adam
2019 年 9 月 19 日
編集済み: Adam
2019 年 9 月 19 日
Creating functions is the way to go definitely, as you appear to have already done in one case of the pasted code, although whether you can define a function in the middle of a script or not I'm not entirely sure, I've never tried. You can define them all at the end certainly, or in their own file.
You also need to take care of input arguments though unless you use nested functions, which I'm fairly sure you can only do if the top level if is a function, not a script. Otherwise you'd need to pass the arguments in to each function.
採用された回答
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!