- works well if you replace If by if
- Matlab is case sensitive
- keep an eye on the little box in the upper right corner of editor window. Red indicates syntax error.
Info
This question is closed. Reopen it to edit or answer.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
I need help with an if statement problem.
1 view (last 30 days)
Show older comments
I am working on a Matlab project for a class. I am still relatively new to Matlab but can somewhat comprehend a bit of it. My project is a game picker for if you want to pick a video game. I am making a smaller version of it then what it could be. It will most likely consist of a lot of if statements. Here is what I have so far:
Y=1
N=2
step1=input('Do you Want to play a game?\n Y or N\n');
If step1==N
fprintf('Then do something else because I cannot help with that')
else
step2=input('Do you know the name of the game you want to play?\n Y or N\n')
end
Any help is appreciated. I just want to be able to understand this stuff better.
Thank you
14 Comments
per isakson
on 27 Nov 2015
Edited: per isakson
on 27 Nov 2015
Ian Meister
on 27 Nov 2015
Thank you so much, I actually figured it out about five minutes after posting. Now I'm just trying to figure out if you can input and output text like the name of a game.
Walter Roberson
on 27 Nov 2015
playername = input('What is your name?', 's');
Star Strider
on 27 Nov 2015
Since you likely don’t have every game, expanding on Walter’s Comment, consider listdlg for the choices.
Ian Meister
on 27 Nov 2015
Is there any way I can change the following code to display it using the fprintf or another function?
result1=input('enter the name of the game you want to play\n','s');
clc
fprintf('Congratulations you want to play %6.2','result1')
Star Strider
on 27 Nov 2015
You need one of the input functions to get the appropriate information into your script. The fprintf call is correct, although you need to terminate the string with a carriage return and linefeed (\n):
fprintf('Congratulations you want to play %6.2\n','result1')
I would use the GUI functions such as inputdlg, listdlg, msgbox, questdlg, and others. They’re simply easier to program, and for the user to interact with. They also don’t mess up your Command Window. They require a minimal acquaintance with cell variables, but those are important to learn to use, regardless.
Ian Meister
on 27 Nov 2015
Edited: Ian Meister
on 27 Nov 2015
How could I go about using msgbox, it looks better and like you said is easier to interact with? This is what I have so far:
result1=input('enter the name of the game you want to play\n','s');
clc
s=msgbox('Congratulations you want to play %6.2','result1')
Walter Roberson
on 27 Nov 2015
s = msgbox( sprintf('Congratulations you want to play %6.2s',result1) );
but using a %6.2s format is a pretty unlikely thing to do, even if it is technically valid.
Ian Meister
on 27 Nov 2015
Thank you for the help. I ended up using:
result1=msgbox({'Congratulations you want to play', input('Insert the name of the game you want to play: ','s')});
This did what I wanted it to do and works which is great.
Ian Meister
on 27 Nov 2015
Alright So I've moved on from this part of the code and am working on a genre selection part. Is there a way to make a dialogue box come up which would display four different buttons each representing a different genre?
Star Strider
on 27 Nov 2015
I would use inputdlg rather than input. For the genre selection, questdlg is likely best.
Ian Meister
on 27 Nov 2015
step3=questdlg('Choose your games genre','Genre','Genre 1','Genre 2','Genre 3','Genre 4');
Would this work? I tested that it would pop up as a display box but haven't tested if it is functional as an input.
Ian Meister
on 27 Nov 2015
I just realized that the separate genres have no meaning behind them. They're just buttons. Do I need to add a variable to the genre name and then use the variable name as the button?
Walter Roberson
on 27 Nov 2015
"need"... maybe not. But it would be a good idea even if you do not need it.
Answers (0)
See Also
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)