How to Plot a graph, from selected items of columns in Matlab appdesigner.

2 次查看(过去 30 天)
I have created the app which reads a '*.xlsx' file into UITable. There are 2 dropdown options to select particular column of UITable. Lets say Dropdown 1 = x column, Dropdown 2 = ycolumn, I need to plot a graph using values from 100th row to till 200th row only of both columns. Please explain with an example.

回答(1 个)

Deepak
Deepak 2024-8-29
Hi @veeresh nashimath, from my understanding, you have an app designer code that reads a “.xlsx” file and displays the data in a UI Table. The app has two dropdowns named “x-axis” and “y-axis” that show two columns of UI Table. Now, you want to plot the dropdown data from the 100th row to the 200th row on the push button callback.
To do this, we can modify the “PlotButtonPushed” callback to use the “FromEditField” and “ToEditField” inputs. These inputs will determine the range of rows to be used for plotting the selected columns from the UI Table. We can then pass the “FromRow” and “ToRow” row numbers to “table2array” method to convert the required data rows into arrays to plot. Finally, we can use “plot” method to create the desired graph.
Here is the MATLAB code for “PlotButtonPushed” method:
function PlotButtonPushed(app, event)
% Get the selected range from the edit fields
fromRow = app.FromEditField.Value;
toRow = app.ToEditField.Value;
% Get the selected columns from the dropdowns
xColumnName = app.xAxisDropDown.Value;
yColumnName = app.yAxisDropDown.Value;
% Ensure the range is valid
if fromRow < 1 || toRow > height(app.UITable.Data) || fromRow > toRow
uialert(app.UIFigure, 'Invalid row range specified.', 'Error');
return;
end
% Extract the data for the specified range and columns
xData = table2array(app.UITable.Data(fromRow:toRow, xColumnName));
yData = table2array(app.UITable.Data(fromRow:toRow, yColumnName));
plot(app.UIAxes, xData, yData);
xlabel(app.UIAxes, xColumnName);
ylabel(app.UIAxes, yColumnName);
title(app.UIAxes, sprintf('Plot of %s vs %s', yColumnName, xColumnName));
end
Below is the documentation of “table2array” method for reference:
I hope this will solve the problem.

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by