get input dialog for a time value (t) using in physic problem

조회 수: 6 (최근 30일)
Tuan
Tuan 2022년 8월 5일
답변: Shlok 2025년 4월 29일
I have a physics excercise, which requires me to get input from user for each value to calculate the math problem. My question is how can I get a range value of double (integer etc...) throw a input dialog function , please help me with this!

답변 (1개)

Shlok
Shlok 2025년 4월 29일
Hi Tuan,
As per my understanding, you want to prompt the user to input multiple values one at a time.
This can be achieved by using a loop with the "inputdlg" function to collect each value individually and store them in an array. The "inputdlg" function in MATLAB creates a pop-up dialog box that prompts the user to input text data. You can use it inside a loop to repeatedly ask for different values. Here is a sample code to achieve the same:
n = 5; % Number of time values
t = zeros(1, n); % Preallocate array for inputs
for i = 1:n
prompt = {sprintf('Enter time value #%d:', i)};
dlg_title = 'Input Time';
dims = [1 35];
definput = {'0'}; % Default input
answer = inputdlg(prompt, dlg_title, dims, definput);
if isempty(answer) % If user clicks on cancel button
error('User cancelled the input.');
end
t(i) = str2double(answer{1});
if isnan(t(i))
error('Invalid input. Please enter a numeric value.');
end
end
disp('Time values entered:');
disp(t);
To know more about "inputdlg" function, refer to the following MathWorks Documentation link:

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by