How can I calculate the value in imported table using App Designer?

5 次查看(过去 30 天)
Hello, I have basic knowledge in MATLAB. Basically, I would like to calculate the uitable in App Designer by input few values and add a new column as "new value" by using push button (calculate).From a new column, i can plot the graph. I have added the callback function. However, only xls.data can be imported but the calculate button is not working. Here my code for callback function:
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
end
% Button pushed function: ImportDataButton
function ImportDataButtonPushed(app, event)
t = readtable("densitydata.xlsx","sheet",1);
app.UITable_2.Data = t;
t.Properties.VariableNames{1} = 'Depth';
t.Properties.VariableNames{2} = 'Log Data';
app.UITable_2.ColumnName = t.Properties.VariableNames;
end
% Callback function: CalculateButton, UITable_2, UITable_2
function CalculateButtonPushed(app, event)
d = app.DrillingFluidEditField.Value;
m = app.MatrixDensityEditField.Value;
l = app.UITable_2.ColumnName{2};
n = (m-l)/(m-d);
app.UITable.Visible = n;
x = table2array(t(:,"Porosity"));
y = table2array(t(:,"Depth"));
plot(app.UIAxes,x,y);
end
% Callback function
function UITableDisplayDataChanged(app, event)
displayNewdata = app.UITable.DisplayData;
app.UITable.Visible = app.CalculateButton.ButtonPushedFcn;
end
end
Your help means a lot to me. Thank you.

回答(1 个)

Omega
Omega 2025-5-13
Hi Aina,
There are some issues with your code. I have listed them below:
  • The table "t" loaded in "ImportDataButtonPushed" is not accessible in "CalculateButtonPushed" (it's a local variable).
  • "app.UITable_2.ColumnName{2}" is a string, not the data itself.
  • You need to add a new column to the table, then update the "UITable_2.Data".
  • You should use the updated table data for plotting.
You can refer to the following code as a reference to address these issues:
function ImportDataButtonPushed(app, event)
t = readtable("densitydata.xlsx", "Sheet", 1);
t.Properties.VariableNames{1} = 'Depth';
t.Properties.VariableNames{2} = 'LogData';
app.UITable.Data = t;
end
function CalculateButtonPushed(app, event)
d = app.DrillingFluidEditField.Value;
m = app.MatrixDensityEditField.Value;
t = app.UITable.Data; % Get table from UI component
% Calculate porosity
porosity = (m - t.LogData) / (m - d);
t.Porosity = porosity; % Add new column
app.UITable.Data = t; % Update table in UI
% Plot Porosity vs Depth
plot(app.UIAxes, t.Porosity, t.Depth, '-o');
xlabel(app.UIAxes, 'Porosity');
ylabel(app.UIAxes, 'Depth');
set(app.UIAxes, 'YDir', 'reverse'); % For depth plots
end
I hope it helps!

类别

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

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by