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

조회 수: 4 (최근 30일)
Aina Afiqah
Aina Afiqah 2022년 1월 23일
답변: Omega 2025년 5월 13일
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