Clear Filters
Clear Filters

why do I receive error (Index in position 2 exceeds array bounds. Index must not exceed 1.) when I put different sizes like (2x2)

2 views (last 30 days)
classdef PANCITCANTON_exported < matlab.ui.componentcontainer.ComponentContainer
% Properties that correspond to underlying components
properties (Access = private, Transient, NonCopyable)
OPERATIONSPanel matlab.ui.container.Panel
MIN matlab.ui.control.Button
TRANS matlab.ui.control.Button
INVB matlab.ui.control.Button
INVA matlab.ui.control.Button
DETA matlab.ui.control.Button
CLEAR matlab.ui.control.Button
DETB matlab.ui.control.Button
MULT matlab.ui.control.Button
ADD matlab.ui.control.Button
MATRIXCPanel matlab.ui.container.Panel
C13 matlab.ui.control.NumericEditField
C33 matlab.ui.control.NumericEditField
C23 matlab.ui.control.NumericEditField
C32 matlab.ui.control.NumericEditField
C22 matlab.ui.control.NumericEditField
C12 matlab.ui.control.NumericEditField
C31 matlab.ui.control.NumericEditField
C21 matlab.ui.control.NumericEditField
C11 matlab.ui.control.NumericEditField
MATRIXBPanel matlab.ui.container.Panel
B11 matlab.ui.control.NumericEditField
B33 matlab.ui.control.NumericEditField
B23 matlab.ui.control.NumericEditField
B13 matlab.ui.control.NumericEditField
B32 matlab.ui.control.NumericEditField
B22 matlab.ui.control.NumericEditField
B12 matlab.ui.control.NumericEditField
B31 matlab.ui.control.NumericEditField
B21 matlab.ui.control.NumericEditField
MATRIXAPanel matlab.ui.container.Panel
A23 matlab.ui.control.NumericEditField
A31 matlab.ui.control.NumericEditField
A21 matlab.ui.control.NumericEditField
A11 matlab.ui.control.NumericEditField
A33 matlab.ui.control.NumericEditField
A13 matlab.ui.control.NumericEditField
A32 matlab.ui.control.NumericEditField
A22 matlab.ui.control.NumericEditField
A12 matlab.ui.control.NumericEditField
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: A11
function A11ValueChanged(comp, ~)
global A
A(1,1) = comp.A11.Value;
end
% Value changed function: A12
function A12ValueChanged(comp, ~)
global A
A(1,2)= comp.A12.Value;
end
% Value changed function: A13
function A13ValueChanged(comp, ~)
global A
A(1,3) = comp.A13.Value;
end
% Value changed function: A21
function A21ValueChanged(comp, ~)
global A
A(2,1) = comp.A21.Value;
end
% Value changed function: A22
function A22ValueChanged(comp, ~)
global A
A(2,2) = comp.A22.Value;
end
% Value changed function: A23
function A23ValueChanged(comp, ~)
global A
A(2,3) = comp.A23.Value;
end
% Value changed function: A31
function A31ValueChanged(comp, ~)
global A
A(3,1) = comp.A31.Value;
end
% Value changed function: A32
function A32ValueChanged(comp, ~)
global A
A(3,2) = comp.A32.Value;
end
% Value changed function: A33
function A33ValueChanged(comp, ~)
global A
A(3,3) = comp.A33.Value;
end
% Value changed function: B11
function B11ValueChanged(comp, ~)
global B
B(1,1) = comp.B11.Value;
end
% Value changed function: B12
function B12ValueChanged(comp, ~)
global B
B(1,2) = comp.B12.Value;
end
% Value changed function: B13
function B13ValueChanged(comp, ~)
global B
B(1,3) = comp.B13.Value;
end
% Value changed function: B21
function B21ValueChanged(comp, ~)
global B
B(2,1) = comp.B21.Value;
end
% Value changed function: B22
function B22ValueChanged(comp, ~)
global B
B(2,2) = comp.B22.Value;
end
% Value changed function: B23
function B23ValueChanged(comp, ~)
global B
B(2,3) = comp.B23.Value;
end
% Value changed function: B31
function B31ValueChanged(comp, ~)
global B
B(3,1) = comp.B31.Value;
end
% Value changed function: B32
function B32ValueChanged(comp, ~)
global B
B(3,2) = comp.B32.Value;
end
% Value changed function: B33
function B33ValueChanged(comp, ~)
global B
B(3,3) = comp.B33.Value;
end
% Value changed function: C11
function C11ValueChanged(comp, ~)
global C
C(1,1) = comp.C11.Value;
end
% Value changed function: C12
function C12ValueChanged(comp, ~)
global C
C(1,2) = comp.C12.Value;
end
% Value changed function: C13
function C13ValueChanged(comp, ~)
global C
C(1,3) = comp.C13.Value;
end
% Value changed function: C21
function C21ValueChanged(comp, ~)
global C
C(2,1) = comp.C21.Value;
end
% Value changed function: C22
function C22ValueChanged(comp, ~)
global C
C(2,2) = comp.C22.Value;
end
% Value changed function: C23
function C23ValueChanged(comp, ~)
global C
C(2,3) = comp.C23.Value;
end
% Value changed function: C31
function C31ValueChanged(comp, ~)
global C
C(3,1) = comp.C31.Value;
end
% Value changed function: C32
function C32ValueChanged(comp, ~)
global C
C(3,2) = comp.C32.Value;
end
% Value changed function: C33
function C33ValueChanged(comp, ~)
global C
C(3,3) = comp.C33.Value;
end
% Button pushed function: ADD
function ADDButtonPushed(comp, ~)
global A
global B
global C
C=A+B;
comp.C11.Value=C(1,1);
comp.C12.Value=C(1,2);
comp.C13.Value=C(1,3);
comp.C21.Value=C(2,1);
comp.C22.Value=C(2,2);
comp.C23.Value=C(2,3);
comp.C31.Value=C(3,1);
comp.C32.Value=C(3,2);
comp.C33.Value=C(3,3);
end
% Callback function
function EXEButtonPushed(comp, ~)
matrices = [comp.A11.Value, comp.A12.Value, comp.A13.Value, comp.A21.Value, comp.A22.Value, comp.A23.Value, comp.A31.Value, comp.A32.Value, comp.A33.Value];
end
% Button pushed function: MIN
function MINButtonPushed(comp, ~)
global A
global B
global C
C=A-B;
comp.C11.Value=C(1,1);
comp.C12.Value=C(1,2);
comp.C13.Value=C(1,3);
comp.C21.Value=C(2,1);
comp.C22.Value=C(2,2);
comp.C23.Value=C(2,3);
comp.C31.Value=C(3,1);
comp.C32.Value=C(3,2);
comp.C33.Value=C(3,3);
end
% Button pushed function: MULT
function MULTButtonPushed(comp, ~)
global A
global B
global C
C=A.*B;
comp.C11.Value=C(1,1);
comp.C12.Value=C(1,2);
comp.C13.Value=C(1,3);
comp.C21.Value=C(2,1);
comp.C22.Value=C(2,2);
comp.C23.Value=C(2,3);
comp.C31.Value=C(3,1);
comp.C32.Value=C(3,2);
comp.C33.Value=C(3,3);
end
% Button pushed function: CLEAR
function CLEARButtonPushed(comp, ~)
comp.A11.Value = 0;
comp.A12.Value = 0;
comp.A13.Value = 0;
comp.A21.Value = 0;
comp.A22.Value = 0;
comp.A23.Value = 0;
comp.A31.Value = 0;
comp.A32.Value = 0;
comp.A33.Value = 0;
comp.B11.Value = 0;
comp.B12.Value = 0;
comp.B13.Value = 0;
comp.B21.Value = 0;
comp.B22.Value = 0;
comp.B23.Value = 0;
comp.B31.Value = 0;
comp.B32.Value = 0;
comp.B33.Value = 0;
comp.C11.Value = 0;
comp.C12.Value = 0;
comp.C13.Value = 0;
comp.C21.Value = 0;
comp.C22.Value = 0;
comp.C23.Value = 0;
comp.C31.Value = 0;
comp.C32.Value = 0;
comp.C33.Value = 0;
clear;
clc;
end
% Button pushed function: INVA
function INVAButtonPushed(comp, ~)
global A
global C
C=inv(A);
comp.C11.Value=C(1,1);
comp.C12.Value=C(1,2);
comp.C13.Value=C(1,3);
comp.C21.Value=C(2,1);
comp.C22.Value=C(2,2);
comp.C23.Value=C(2,3);
comp.C31.Value=C(3,1);
comp.C32.Value=C(3,2);
comp.C33.Value=C(3,3);
end
% Button pushed function: DETA
function DETAButtonPushed(comp, ~)
comp.C11.Value=C(1,1);
comp.C12.Value=C(1,2);
comp.C13.Value=C(1,3);
comp.C21.Value=C(2,1);
comp.C22.Value=C(2,2);
comp.C23.Value=C(2,3);
comp.C31.Value=C(3,1);
comp.C32.Value=C(3,2);
comp.C33.Value=C(3,3);
end
% Button pushed function: TRANS
function TRANSButtonPushed(comp, ~)
global A
global B
global C
C=A';
C=B';
comp.C11.Value=C(1,1);
comp.C12.Value=C(1,2);
comp.C13.Value=C(1,3);
comp.C21.Value=C(2,1);
comp.C22.Value=C(2,2);
comp.C23.Value=C(2,3);
comp.C31.Value=C(3,1);
comp.C32.Value=C(3,2);
comp.C33.Value=C(3,3);
end
end
methods (Access = protected)
% Code that executes when the value of a public property is changed
function update(comp)
% Use this function to update the underlying components
end
% Create the underlying components
function setup(comp)
comp.Position = [1 1 849 576];
comp.BackgroundColor = [0.94 0.94 0.94];
% Create MATRIXAPanel
comp.MATRIXAPanel = uipanel(comp);
comp.MATRIXAPanel.Title = 'MATRIX A';
comp.MATRIXAPanel.BackgroundColor = [0.0588 1 1];
comp.MATRIXAPanel.Position = [32 282 349 214];
% Create A12
comp.A12 = uieditfield(comp.MATRIXAPanel, 'numeric');
comp.A12.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @A12ValueChanged, true);
comp.A12.Position = [140 127 69 30];
% Create A22
comp.A22 = uieditfield(comp.MATRIXAPanel, 'numeric');
comp.A22.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @A22ValueChanged, true);
comp.A22.Position = [140 77 69 28];
% Create A32
comp.A32 = uieditfield(comp.MATRIXAPanel, 'numeric');
comp.A32.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @A32ValueChanged, true);
comp.A32.Position = [140 31 69 30];
% Create A13
comp.A13 = uieditfield(comp.MATRIXAPanel, 'numeric');
comp.A13.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @A13ValueChanged, true);
comp.A13.Position = [259 129 69 28];
% Create A33
comp.A33 = uieditfield(comp.MATRIXAPanel, 'numeric');
comp.A33.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @A33ValueChanged, true);
comp.A33.Position = [259 33 69 28];
% Create A11
comp.A11 = uieditfield(comp.MATRIXAPanel, 'numeric');
comp.A11.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @A11ValueChanged, true);
comp.A11.Position = [19 127 69 30];
% Create A21
comp.A21 = uieditfield(comp.MATRIXAPanel, 'numeric');
comp.A21.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @A21ValueChanged, true);
comp.A21.Position = [18 76 69 31];
% Create A31
comp.A31 = uieditfield(comp.MATRIXAPanel, 'numeric');
comp.A31.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @A31ValueChanged, true);
comp.A31.Position = [18 31 69 30];
% Create A23
comp.A23 = uieditfield(comp.MATRIXAPanel, 'numeric');
comp.A23.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @A23ValueChanged, true);
comp.A23.Position = [259 75 69 30];
% Create MATRIXBPanel
comp.MATRIXBPanel = uipanel(comp);
comp.MATRIXBPanel.Title = 'MATRIX B';
comp.MATRIXBPanel.BackgroundColor = [0 1 0];
comp.MATRIXBPanel.Position = [465 282 349 214];
% Create B21
comp.B21 = uieditfield(comp.MATRIXBPanel, 'numeric');
comp.B21.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @B21ValueChanged, true);
comp.B21.Position = [16 77 69 31];
% Create B31
comp.B31 = uieditfield(comp.MATRIXBPanel, 'numeric');
comp.B31.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @B31ValueChanged, true);
comp.B31.Position = [16 31 69 30];
% Create B12
comp.B12 = uieditfield(comp.MATRIXBPanel, 'numeric');
comp.B12.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @B12ValueChanged, true);
comp.B12.Position = [142 127 69 30];
% Create B22
comp.B22 = uieditfield(comp.MATRIXBPanel, 'numeric');
comp.B22.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @B22ValueChanged, true);
comp.B22.Position = [142 77 69 29];
% Create B32
comp.B32 = uieditfield(comp.MATRIXBPanel, 'numeric');
comp.B32.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @B32ValueChanged, true);
comp.B32.Position = [142 29 69 32];
% Create B13
comp.B13 = uieditfield(comp.MATRIXBPanel, 'numeric');
comp.B13.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @B13ValueChanged, true);
comp.B13.Position = [253 127 68 30];
% Create B23
comp.B23 = uieditfield(comp.MATRIXBPanel, 'numeric');
comp.B23.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @B23ValueChanged, true);
comp.B23.Position = [253 77 68 30];
% Create B33
comp.B33 = uieditfield(comp.MATRIXBPanel, 'numeric');
comp.B33.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @B33ValueChanged, true);
comp.B33.Position = [253 29 69 30];
% Create B11
comp.B11 = uieditfield(comp.MATRIXBPanel, 'numeric');
comp.B11.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @B11ValueChanged, true);
comp.B11.Position = [16 127 69 30];
% Create MATRIXCPanel
comp.MATRIXCPanel = uipanel(comp);
comp.MATRIXCPanel.Title = 'MATRIX C';
comp.MATRIXCPanel.BackgroundColor = [0.0588 1 1];
comp.MATRIXCPanel.Position = [32 44 349 214];
% Create C11
comp.C11 = uieditfield(comp.MATRIXCPanel, 'numeric');
comp.C11.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @C11ValueChanged, true);
comp.C11.Position = [18 141 69 30];
% Create C21
comp.C21 = uieditfield(comp.MATRIXCPanel, 'numeric');
comp.C21.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @C21ValueChanged, true);
comp.C21.Position = [18 90 69 30];
% Create C31
comp.C31 = uieditfield(comp.MATRIXCPanel, 'numeric');
comp.C31.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @C31ValueChanged, true);
comp.C31.Position = [18 35 69 30];
% Create C12
comp.C12 = uieditfield(comp.MATRIXCPanel, 'numeric');
comp.C12.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @C12ValueChanged, true);
comp.C12.Position = [140 141 69 30];
% Create C22
comp.C22 = uieditfield(comp.MATRIXCPanel, 'numeric');
comp.C22.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @C22ValueChanged, true);
comp.C22.Position = [140 88 69 32];
% Create C32
comp.C32 = uieditfield(comp.MATRIXCPanel, 'numeric');
comp.C32.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @C32ValueChanged, true);
comp.C32.Position = [140 35 69 29];
% Create C23
comp.C23 = uieditfield(comp.MATRIXCPanel, 'numeric');
comp.C23.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @C23ValueChanged, true);
comp.C23.Position = [259 88 69 32];
% Create C33
comp.C33 = uieditfield(comp.MATRIXCPanel, 'numeric');
comp.C33.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @C33ValueChanged, true);
comp.C33.Position = [259 35 69 29];
% Create C13
comp.C13 = uieditfield(comp.MATRIXCPanel, 'numeric');
comp.C13.ValueChangedFcn = matlab.apps.createCallbackFcn(comp, @C13ValueChanged, true);
comp.C13.Position = [259 141 69 30];
% Create OPERATIONSPanel
comp.OPERATIONSPanel = uipanel(comp);
comp.OPERATIONSPanel.TitlePosition = 'centertop';
comp.OPERATIONSPanel.Title = 'OPERATIONS';
comp.OPERATIONSPanel.Position = [465 44 143 214];
% Create ADD
comp.ADD = uibutton(comp.OPERATIONSPanel, 'push');
comp.ADD.ButtonPushedFcn = matlab.apps.createCallbackFcn(comp, @ADDButtonPushed, true);
comp.ADD.FontSize = 9;
comp.ADD.Position = [19 166 105 22];
comp.ADD.Text = '+';
% Create MULT
comp.MULT = uibutton(comp.OPERATIONSPanel, 'push');
comp.MULT.ButtonPushedFcn = matlab.apps.createCallbackFcn(comp, @MULTButtonPushed, true);
comp.MULT.FontSize = 9;
comp.MULT.Position = [19 114 105 21];
comp.MULT.Text = '*';
% Create DETB
comp.DETB = uibutton(comp.OPERATIONSPanel, 'push');
comp.DETB.FontSize = 9;
comp.DETB.Position = [82 32 42 22];
comp.DETB.Text = 'DET B';
% Create CLEAR
comp.CLEAR = uibutton(comp.OPERATIONSPanel, 'push');
comp.CLEAR.ButtonPushedFcn = matlab.apps.createCallbackFcn(comp, @CLEARButtonPushed, true);
comp.CLEAR.FontSize = 9;
comp.CLEAR.Position = [19 7 105 20];
comp.CLEAR.Text = 'CLEAR';
% Create DETA
comp.DETA = uibutton(comp.OPERATIONSPanel, 'push');
comp.DETA.ButtonPushedFcn = matlab.apps.createCallbackFcn(comp, @DETAButtonPushed, true);
comp.DETA.FontSize = 9;
comp.DETA.Position = [19 33 42 22];
comp.DETA.Text = 'DET A';
% Create INVA
comp.INVA = uibutton(comp.OPERATIONSPanel, 'push');
comp.INVA.ButtonPushedFcn = matlab.apps.createCallbackFcn(comp, @INVAButtonPushed, true);
comp.INVA.FontSize = 9;
comp.INVA.Position = [19 88 42 22];
comp.INVA.Text = 'INV A';
% Create INVB
comp.INVB = uibutton(comp.OPERATIONSPanel, 'push');
comp.INVB.FontSize = 9;
comp.INVB.Position = [81 88 42 22];
comp.INVB.Text = 'INV B';
% Create TRANS
comp.TRANS = uibutton(comp.OPERATIONSPanel, 'push');
comp.TRANS.ButtonPushedFcn = matlab.apps.createCallbackFcn(comp, @TRANSButtonPushed, true);
comp.TRANS.FontSize = 9;
comp.TRANS.Position = [20 61 104 21];
comp.TRANS.Text = 'TRANS';
% Create MIN
comp.MIN = uibutton(comp.OPERATIONSPanel, 'push');
comp.MIN.ButtonPushedFcn = matlab.apps.createCallbackFcn(comp, @MINButtonPushed, true);
comp.MIN.FontSize = 9;
comp.MIN.Position = [19 141 105 21];
comp.MIN.Text = '-';
end
end
end

Answers (1)

Fifteen12
Fifteen12 on 8 Dec 2022
The error your getting is raised when you try to access part of an array that isn't declared. For example:
C = ['a', 'b', 'c'];
% I can do the following:
C(1); % returns 'a'
C(2); % returns 'b'
C(3); % returns 'c'
% But if I try to access the 4th element (not defined):
C(4)
Index exceeds the number of array elements. Index must not exceed 3.
So in your case you have a variable defined that only has one element in it, and you're trying to access it in the the 2nd row, 2nd column.
I would look at what you expect C to be, and how you define A and B in the defining functions.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!