How to store arrays from this for loop and merge them to be displayed on an App Designer Table?
2 次查看(过去 30 天)
显示 更早的评论
Hello, I am trying to make a 3x3 Gaussian-Seidel calculator, I'm having a problem trying to store the array of the rows and columns so it can be displayed on an App Designer Table. The table will be measured by iteration, then the values for each iteration will be displayed.
classdef LE4 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
UITable matlab.ui.control.Table
D1 matlab.ui.control.NumericEditField
D3 matlab.ui.control.NumericEditField
D2 matlab.ui.control.NumericEditField
X31 matlab.ui.control.NumericEditField
X33 matlab.ui.control.NumericEditField
X32 matlab.ui.control.NumericEditField
X21 matlab.ui.control.NumericEditField
X23 matlab.ui.control.NumericEditField
X22 matlab.ui.control.NumericEditField
X13 matlab.ui.control.NumericEditField
X12 matlab.ui.control.NumericEditField
LABEL_RESULT matlab.ui.control.Label
LABEL_Columns matlab.ui.control.Label
LABEL_ROWS matlab.ui.control.Label
LABEL_INSTRUCTIONS matlab.ui.control.Label
LABEL_DETERMINANT matlab.ui.control.Label
LABEL_CREDITS2 matlab.ui.control.Label
LABEL_CREDITS matlab.ui.control.Label
LABEL_PROGRAMNAME matlab.ui.control.Label
CalculateButton matlab.ui.control.Button
X11 matlab.ui.control.NumericEditField
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
clc
A = [app.X11.Value app.X12.Value app.X13.Value;app.X21.Value app.X22.Value app.X23.Value;app.X31.Value app.X32.Value app.X33.Value];
b = [app.D1.Value app.D2.Value app.D3.Value]';
x = [0 0 0]';
n=size(x,1);
normVal=Inf;
tol=1e-5; itr=0;
while normVal>tol
x_old=x;
for i=1:n sigma=0;
for j=1:i-1 sigma=sigma+A(i,j)*x(j);
end
for j=i+1:n sigma=sigma+A(i,j)*x_old(j);
end
x(i)=(1/A(i,i))*(b(i)-sigma);
end
itr=itr+1;
normVal=norm(x_old-x);
tdata=app.UITable
tdata = table(itr, A:1, A:2, A:3),'VariableNames',{'K','X1','X2','X3'};
%app.UITable.Data = itr, x(:,1), x(:,2), X(:,3)
fprintf('Solution of the system is : \n%f \n%f \n%f \n%f in %d iterations',x,itr);
end
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create X11
app.X11 = uieditfield(app.UIFigure, 'numeric');
app.X11.Position = [70 346 26 22];
% Create CalculateButton
app.CalculateButton = uibutton(app.UIFigure, 'push');
app.CalculateButton.ButtonPushedFcn = createCallbackFcn(app, @CalculateButtonPushed, true);
app.CalculateButton.Position = [104 230 100 22];
app.CalculateButton.Text = 'Calculate';
% Create LABEL_PROGRAMNAME
app.LABEL_PROGRAMNAME = uilabel(app.UIFigure);
app.LABEL_PROGRAMNAME.HorizontalAlignment = 'right';
app.LABEL_PROGRAMNAME.Position = [28 447 250 22];
app.LABEL_PROGRAMNAME.Text = '3x3 Gauss-Seidel Calculator (Constants only)';
% Create LABEL_CREDITS
app.LABEL_CREDITS = uilabel(app.UIFigure);
app.LABEL_CREDITS.HorizontalAlignment = 'right';
app.LABEL_CREDITS.Position = [218 34 137 22];
app.LABEL_CREDITS.Text = 'A program presented by:';
% Create LABEL_CREDITS2
app.LABEL_CREDITS2 = uilabel(app.UIFigure);
app.LABEL_CREDITS2.HorizontalAlignment = 'right';
app.LABEL_CREDITS2.Position = [218 13 212 22];
app.LABEL_CREDITS2.Text = 'Dablo, Peralta, Pontejo, Tero, Villegas.';
% Create LABEL_DETERMINANT
app.LABEL_DETERMINANT = uilabel(app.UIFigure);
app.LABEL_DETERMINANT.HorizontalAlignment = 'right';
app.LABEL_DETERMINANT.Position = [195 367 71 22];
app.LABEL_DETERMINANT.Text = 'Determinant';
% Create LABEL_INSTRUCTIONS
app.LABEL_INSTRUCTIONS = uilabel(app.UIFigure);
app.LABEL_INSTRUCTIONS.HorizontalAlignment = 'right';
app.LABEL_INSTRUCTIONS.Position = [34 401 240 22];
app.LABEL_INSTRUCTIONS.Text = 'Please input the required value for the table';
% Create LABEL_ROWS
app.LABEL_ROWS = uilabel(app.UIFigure);
app.LABEL_ROWS.HorizontalAlignment = 'right';
app.LABEL_ROWS.Position = [10 346 49 22];
app.LABEL_ROWS.Text = 'M Rows';
% Create LABEL_Columns
app.LABEL_Columns = uilabel(app.UIFigure);
app.LABEL_Columns.HorizontalAlignment = 'right';
app.LABEL_Columns.Position = [50 367 65 22];
app.LABEL_Columns.Text = 'N Columns';
% Create LABEL_RESULT
app.LABEL_RESULT = uilabel(app.UIFigure);
app.LABEL_RESULT.HorizontalAlignment = 'right';
app.LABEL_RESULT.Position = [293 447 40 22];
app.LABEL_RESULT.Text = 'Result';
% Create X12
app.X12 = uieditfield(app.UIFigure, 'numeric');
app.X12.Position = [70 312 26 22];
% Create X13
app.X13 = uieditfield(app.UIFigure, 'numeric');
app.X13.Position = [70 278 26 22];
% Create X22
app.X22 = uieditfield(app.UIFigure, 'numeric');
app.X22.Position = [114 312 26 22];
% Create X23
app.X23 = uieditfield(app.UIFigure, 'numeric');
app.X23.Position = [114 278 26 22];
% Create X21
app.X21 = uieditfield(app.UIFigure, 'numeric');
app.X21.Position = [114 346 26 22];
% Create X32
app.X32 = uieditfield(app.UIFigure, 'numeric');
app.X32.Position = [163 312 26 22];
% Create X33
app.X33 = uieditfield(app.UIFigure, 'numeric');
app.X33.Position = [163 278 26 22];
% Create X31
app.X31 = uieditfield(app.UIFigure, 'numeric');
app.X31.Position = [163 346 26 22];
% Create D2
app.D2 = uieditfield(app.UIFigure, 'numeric');
app.D2.Position = [218 312 26 22];
% Create D3
app.D3 = uieditfield(app.UIFigure, 'numeric');
app.D3.Position = [218 278 26 22];
% Create D1
app.D1 = uieditfield(app.UIFigure, 'numeric');
app.D1.Position = [218 346 26 22];
% Create UITable
app.UITable = uitable(app.UIFigure);
app.UITable.ColumnName = {'K'; 'X1'; 'X2'; 'X3'};
app.UITable.RowName = {};
app.UITable.Position = [303 109 309 293];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = LE4
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
0 个评论
回答(1 个)
Abhinav
2023-7-5
There is a bug in your code try
tdata = table(itr, A:1, A:2, A:3,'VariableNames',{'K','X1','X2','X3'});
Should fix the issue.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!