While Loop help please

2 次查看(过去 30 天)
Paul Nadeau
Paul Nadeau 2020-4-27
回答: Arka 2023-2-9
Can someone please help we with a while loop. I'm trying to be able to get a person to be able to choose another fish for when FishingGuide = 1. So basically after the person as gone through one run, they can have the option to choose another. Please Help!!!
clc
clear
close all
%Introduction to the program
FishingGuide = menu('Welcome to the Florida Fishing Guide! Would you like to learn how to catch some fish?', 'Yes', 'Random','No');
[~,~,DATA] = xlsread('FishingDatabase.xlsx');
% Ending fucntion if person declines.
if FishingGuide == 3
uiwait(msgbox('Okay, Have a Good Day! If you wish to learn more, vist Florida Fish and Wildlife or myfwc.com for more information!'))
end
%Starting funtion for guide if person selects the option of learning about
%a fish they want to catch.
% Option funtion for it someone chooses to see a random fish data
%elseif changed
if FishingGuide == 2
% FishingGuide = menu('Please select if you would like to learn about multiple fish','Yes(1)','No(0)')
uiwait(msgbox('Here is a guide to catch this fish'))
f = size(DATA, 1)
Random = randi([2, f])
r = DATA(Random, :)
% Refrencing Excel File Columns for Information
Name = r{1,1}
Bait = r{1,5}
LineType = r{1,6}
Reel = r{1,7}
Rod = r{1,8}
Wire = r{1,9}
Style = r{1,10}
% Message box that outputs information for random fish information
H = msgbox(sprintf('Fish is: %s \nThe bait: %s \nLine Type: %s \nReel: %s \nRod: %s \nWire: %s \nStyle: %s',Name,Bait,LineType,Reel,Rod,Wire,Style))
name = r{1,1}
end
% This is going to be the start of specific searching for a fish guide
%elseif
if FishingGuide == 1
Innercoastal = []
Ocean = []
Freshwater = []
m = 1; %Variables for calculations
j = 1; %Variables for calculations
k = 1; %Variables for calculations
for x = length(DATA):-1:1 %Remove's all data with N/A
if strcmpi(DATA(x,2),'N/A') == 0
Innercoastal = [Innercoastal,DATA(x,2)]
InnerDATA(m,:) = DATA(x,:)
m = m+1;
end
if strcmpi(DATA(x,3),'N/A') == 0
Ocean = [Ocean,DATA(x,3)]
OceanDATA(j,:) = DATA(x,:)
j = j+1;
end
if strcmpi(DATA(x,4),'N/A') == 0
Freshwater = [Freshwater,DATA(x,4)]
FreshDATA(k,:) = DATA(x,:)
k = k+1;
end
end
% User interaction for selection the enviornment of the fish they are
% looking for.
A = menu('Which type of fishing are you looking to do?','Innercoastal','Ocean','Freshwater');
uiwait(msgbox('Please select the fish you are looking to catch'))
% col = DATA(:,A+1)
%User selection of enviornment
if A ==1
col = Innercoastal;
elseif A == 2
col = Ocean;
elseif A == 3
col = Freshwater;
end
% List of fish that user can choose
col = menu('Please select the fish you are looking to catch', col)
fishData = DATA(col, :)
%Output information for string variables for Freshwater
if A ==3
name1 = Freshwater{col};
bait = cell2mat(FreshDATA(col,5));
linetype = cell2mat(FreshDATA(col,6));
reel = cell2mat(FreshDATA(col,7));
rod = cell2mat(FreshDATA(col, 8));
wire = cell2mat(FreshDATA(col,9));
style = cell2mat(FreshDATA(col,10));
% Message box that outputs information for catching fish in Freshwater
% enviornment
h = msgbox(sprintf('Fish: %s \nBait: %s \nLine Type: %s \nReel: %s \nRod: %s \nWire Needed: %s \nBest Style: %s',name1,bait,linetype,reel,rod,wire,style))
Plot() %Display;s Bar Chart
end
%Outputs for String variables for Ocean selection
if A == 2
name2 = Ocean{col};
bait2 = cell2mat(OceanDATA(col,5));
linetype2 = cell2mat(OceanDATA(col,6));
reel2 = cell2mat(OceanDATA(col,7));
rod2 = cell2mat(OceanDATA(col, 8));
wire2 = cell2mat(OceanDATA(col,9));
style2 = cell2mat(OceanDATA(col,10));
%Message box that outputs information for catching fish in Ocean
%enviornment
G = msgbox(sprintf('Fish: %s \nBait: %s \nLine Type: %s \nReel: %s \nRod: %s \nWire Needed: %s \nBest Style: %s',name2,bait2,linetype2,reel2,rod2,wire2,style2))
Plot() %Display's Bar Chart
end
% Outputs for String variables for Innercoastal selection
if A == 1
name3 = Innercoastal{col};
bait3 = cell2mat(InnerDATA(col,5));
linetype3 = cell2mat(InnerDATA(col,6));
reel3 = cell2mat(InnerDATA(col,7));
rod3 = cell2mat(InnerDATA(col, 8));
wire3 = cell2mat(InnerDATA(col,9));
style3 = cell2mat(InnerDATA(col,10));
% Message box that outputs information for catching fish in Innercoastal
% environment
K = msgbox(sprintf('Fish: %s \nBait: %s \nLine Type: %s \nReel: %s \nRod: %s \nWire Needed: %s \nBest Style: %s',name3,bait3,linetype3,reel3,rod3,wire3,style3));
Plot() %Display's bar chart
end
end
  1 个评论
Deepak Gupta
Deepak Gupta 2020-4-27
If you use a while loop then everything else will be called from the inside of while loop.
A simple while loop in your case would be:
FishingGuide == 1;
while(FishingGuild==1)
%%When user wants to exit the loop, input should be something else other than 1.
end

请先登录,再进行评论。

回答(1 个)

Arka
Arka 2023-2-9
Hi,
From what I understood, you want to modify the code in such a way that the code runs until the user does not want to choose any more fishes.
You can use a while loop to achieve what you want.
You just need to modify your code in 3 places.
In line 15, before
if FishingGuide == 2
add
while FishingGuide == 1 || FishingGuide == 2
This ensures that the code runs as long as the user does not choose 'No' for the prompt.
In line 128, add
end
This is to close the while loop.
Just before this, in line 127, add the prompt so that the user can choose to either continue, or to exit.
FishingGuide = menu('Would you like to learn how to catch some more fish?', 'Yes', 'Random','No');
I have also attached the complete code with my answer.
You can learn more about while loops from the link given below:

类别

Help CenterFile Exchange 中查找有关 Oceanography and Hydrology 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by