Input a set of values in a form of table

1 view (last 30 days)
Mihajlo Novakovic
Mihajlo Novakovic on 28 May 2015
Answered: Ayush on 4 Dec 2024
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!

Answers (1)

Ayush
Ayush on 4 Dec 2024
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:

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!