Input a set of values in a form of table

조회 수: 2 (최근 30일)
Mihajlo Novakovic
Mihajlo Novakovic 2015년 5월 28일
답변: Ayush 2024년 12월 4일
I want to create a program that will plot curves based on X and Y values that an user inputs. But I am stuck at the very first step. How to make a program offer you a table with two columns in order to input values for x and y. Thank you in advance!

답변 (1개)

Ayush
Ayush 2024년 12월 4일
Hi,
To create a program that allows users to input values for X and Y in a table format, you can utilize the “uifigure” and “uitable” functions. The “uifigure” function creates a figure window for app design, while the “uitable” function generates a table UI component within this window, allowing users to directly enter their data. Refer to an example code below for a better understanding:
function plotInputData
% Create a figure window
fig = uifigure('Name', 'Input X and Y Values', 'Position', [100 100 400 300]);
% Create a table with two columns for X and Y
data = cell(10, 2); % Preallocate for 10 rows
t = uitable(fig, 'Data', data, 'ColumnName', {'X', 'Y'}, ...
'ColumnEditable', [true true], 'Position', [25 75 350 200]);
% Create a button to plot the data
uibutton(fig, 'push', 'Text', 'Plot', ...
'Position', [150 30 100 30], ...
'ButtonPushedFcn', @(btn,event) plotData(t));
% Callback function to plot data
function plotData(table)
% Retrieve data from the table
data = table.Data;
% Convert cell array to numerical array, ignoring empty cells
x = cellfun(@(x) str2double(x), data(:,1), 'UniformOutput', false);
y = cellfun(@(y) str2double(y), data(:,2), 'UniformOutput', false);
% Remove any NaN values resulting from empty cells
x = cell2mat(x(~cellfun('isempty', x)));
y = cell2mat(y(~cellfun('isempty', y)));
% Plot the data
figure;
plot(x, y, '-o');
xlabel('X');
ylabel('Y');
title('User Input Data');
grid on;
end
end
For more information on the “uifigure” and “uitable” functions refer to below documentations:

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by