![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1734708/image.png)
How to create a dialog box in the plot window.
39 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
As a personal project, I have been writing a game engine in Matlab. It will be a sort of text-based RPG, where you input your command into a text box and then the game interprets that. Right now, this is the state of my getCommand function:
function GetCommand(n)
sprintf("GetCommand %d", n)
pause(0.1)
String = input('Input \n', 's');
String = split(String);
Command = String{1,1};
Subject = String{2,1};
switch command
case 'go'
go(subject)
end
The idea is to run Command through a switch function to find that command, and run the command using Subject as its argument.
It currently works well enough. However, the input function requests input directly from Matlab's command line. I would like to request input from the plot window, which is where I'm rendering the game. I found inputdlg(), but that creates an entirely new window, which is not what I want either.
I hope I made myself clear. Is there any way to create a dialog box within the plot window?
0 Commenti
Risposte (1)
Divyajyoti Nayak
il 16 Lug 2024 alle 4:23
Hi @Ruben, you can use MATLAB’s ‘UIControl’ objects to build the user interface of your game. You can learn more about ‘UIControl’ objects from this documentation page:
To make the dialog box you require, you can use the editable field ‘UIControl’ object. Here is a simple code demonstrating it:
clear
clc
f = figure;
Label = uicontrol('Style','text','Units', 'normalized','Position',[0.4, 0.5, 0.2, 0.1]);
EditableField = uicontrol('Style','edit','Units','normalized','Position',[0.4, 0.45, 0.2, 0.1]);
button = uicontrol('Style','pushbutton','String','Enter', 'Units','normalized','Position',[0.45,0.4,0.1,0.05]);
Label.String = "Command Window";
button.Callback = {@onClick, EditableField};
%This function is called when buton is clicked
function onClick(source, event, field)
% Extracting the string from the editable text field
command = field.String;
% Call function to use command here
% Example: function(command)
end
And here’s the output for it:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1734708/image.png)
Hope this helps!
Vedere anche
Categorie
Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!