How to create a dialog box in the plot window.

조회 수: 36 (최근 30일)
Ruben
Ruben 2024년 7월 16일
댓글: Ruben 2024년 7월 16일
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?

채택된 답변

Divyajyoti Nayak
Divyajyoti Nayak 2024년 7월 16일
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:
Hope this helps!
  댓글 수: 1
Ruben
Ruben 2024년 7월 16일
This is awesome! Is there any way to make it so the text is retrieved upon clicking the "Enter" key? Other than that its perfect

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by