# App Error 'value' must be double scalar within the range of 'limits' Can't really find a way to fix

17 views (last 30 days)
Matthew Moniz on 7 Apr 2022
Commented: Matthew Moniz on 8 Apr 2022
Hello All, so my friends and I have been playing the GURPS system for a while, and I'm wanting to make it a better experience, so I'm trying to code a Damage Calculator app (First time doing so) so that we don't have to pull out a calculator and slow down the gameplay. Some background on how the calculator should work. Essentially the player rolls the dice, their roll is subtracted from the enemies damage resistance to give a basic damage, weapon modifiers are multiplied by the basic damage to get weapon mod damage, and (for my homebrew setup) effect damage is added to all of that to get a final damage. Essentially it is Basic damage+Moddamage+Effect damage to get total damage. This should result in a solid number (no decimals) between 1-100 for now. I wrote a code in Matlab code that works well, but since not all the DM's know how to work Matlab, I'm trying to write an application to just plug in numbers for the Roll and Damage Resistance, and select the Modifiers and Effect Damage from Radial Groups. The App works for just calculating the basic damage (so that means my input boxes are the numerical ones), but when I'm trying to add the modifiers or effect damage from the radial groups to the Basic damage, I get an error of 'Value' must be double scalar within the range of 'Limts'. Even when I change the code so that the Mod and Effects are solid numbers, I still get this error. I've tried making the app.mod in the button groups be just a number, I've tried adding app.DAMAGEEditField.Limits=[-100,100]; to the calculation button, I've messed with slr2double in both the mod and effect areas, as well as num2str. Below is my code, any notes or adivise would be helpful.
classdef GurpsDamageAPP < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DAMAGEEditField matlab.ui.control.NumericEditField
DAMAGEEditFieldLabel matlab.ui.control.Label
GurpsSalavgeCharacterDamageCalculatorLabel matlab.ui.control.Label
CalculateButton matlab.ui.control.Button
EffectButtonGroup matlab.ui.container.ButtonGroup
ModifiersButtonGroup matlab.ui.container.ButtonGroup
EnemyDREditField matlab.ui.control.NumericEditField
EnemyDREditFieldLabel matlab.ui.control.Label
PlayerRollEditField matlab.ui.control.NumericEditField
PlayerRollEditFieldLabel matlab.ui.control.Label
end
properties (Access = private)
MOD % Damage Modifiers
EDMG %Effect Damage
end
% Callbacks that handle component events
methods (Access = private)
% Selection changed function: ModifiersButtonGroup
function ModifiersButtonGroupSelectionChanged(app, event)
selectedButton = app.ModifiersButtonGroup.SelectedObject;
if selectedButton==app.CrushingButton
app.MOD =floor(0.5*(app.PlayerRollEditField.Value-app.EnemyDREditField.Value));
elseif selectedButton==app.CuttingButton
app.MOD =floor(1.5*(app.PlayerRollEditField.Value-app.EnemyDREditField.Value));
elseif selectedButton==app.ImpalingButton
app.MOD =2*(app.PlayerRollEditField.Value-app.EnemyDREditField.Value);
elseif selectedButton==app.PiButton
app.MOD =floor(0.5*(app.PlayerRollEditField.Value-app.EnemyDREditField.Value));
elseif selectedButton==app.PiButton_2
app.MOD =1*(app.PlayerRollEditField.Value-app.EnemyDREditField.Value);
elseif selectedButton==app.PiButton_3
app.MOD =floor(1.5*(app.PlayerRollEditField.Value-app.EnemyDREditField.Value));
elseif selectedButton==app.PiButton_4
app.MOD =2*(app.PlayerRollEditField.Value-app.EnemyDREditField.Value);
elseif selectedButton==app.NoneButton
app.MOD =0;
end
end
% Selection changed function: EffectButtonGroup
function EffectButtonGroupSelectionChanged(app, event)
selectedButton = app.EffectButtonGroup.SelectedObject;
if selectedButton==app.BurningButton
app.EDMG =5;
elseif selectedButton==app.PoisonButton
app.EDMG =4;
elseif selectedButton==app.CorrosionButton
app.EDMG =3;
elseif selectedButton==app.ExplosionButton
app.EDMG =6;
elseif selectedButton==app.NoneButton
app.EDMG =0;
end
end
% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
Roll=app.PlayerRollEditField.Value;
DR=app.EnemyDREditField.Value;
Mod=app.MOD;
Edmg=app.EDMG;
D=(Roll-DR)+Mod+Edmg;
app.DAMAGEEditField.Value=str2double(D);
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 484];
app.UIFigure.Name = 'MATLAB App';
% Create PlayerRollEditFieldLabel
app.PlayerRollEditFieldLabel = uilabel(app.UIFigure);
app.PlayerRollEditFieldLabel.HorizontalAlignment = 'right';
app.PlayerRollEditFieldLabel.Position = [103 384 64 22];
app.PlayerRollEditFieldLabel.Text = 'Player Roll';
% Create PlayerRollEditField
app.PlayerRollEditField = uieditfield(app.UIFigure, 'numeric');
app.PlayerRollEditField.Position = [182 384 100 22];
app.PlayerRollEditField.Value = 15;
% Create EnemyDREditFieldLabel
app.EnemyDREditFieldLabel = uilabel(app.UIFigure);
app.EnemyDREditFieldLabel.HorizontalAlignment = 'right';
app.EnemyDREditFieldLabel.Position = [341 384 64 22];
app.EnemyDREditFieldLabel.Text = 'Enemy DR';
% Create EnemyDREditField
app.EnemyDREditField = uieditfield(app.UIFigure, 'numeric');
app.EnemyDREditField.Position = [420 384 100 22];
% Create ModifiersButtonGroup
app.ModifiersButtonGroup = uibuttongroup(app.UIFigure);
app.ModifiersButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ModifiersButtonGroupSelectionChanged, true);
app.ModifiersButtonGroup.Title = 'Modifiers ';
app.ModifiersButtonGroup.Position = [117 213 172 135];
% Create CrushingButton
app.CrushingButton.Text = 'Crushing';
app.CrushingButton.Position = [11 89 73 22];
app.CrushingButton.Value = true;
% Create CuttingButton
app.CuttingButton.Text = 'Cutting';
app.CuttingButton.Position = [11 67 61 22];
% Create ImpalingButton
app.ImpalingButton.Text = 'Impaling';
app.ImpalingButton.Position = [11 45 70 22];
% Create PiButton
app.PiButton.Text = 'Pi(-)';
app.PiButton.Position = [101 88 45 22];
% Create PiButton_2
app.PiButton_2.Text = 'Pi';
app.PiButton_2.Position = [101 65 33 22];
% Create PiButton_3
app.PiButton_3.Text = 'Pi(+)';
app.PiButton_3.Position = [101 19 48 22];
% Create PiButton_4
app.PiButton_4.Text = 'Pi(++)';
app.PiButton_4.Position = [101 44 55 22];
% Create NoneButton
app.NoneButton.Text = 'None';
app.NoneButton.Position = [11 19 52 22];
% Create EffectButtonGroup
app.EffectButtonGroup = uibuttongroup(app.UIFigure);
app.EffectButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @EffectButtonGroupSelectionChanged, true);
app.EffectButtonGroup.Title = 'Effect';
app.EffectButtonGroup.Position = [369 208 123 145];
% Create BurningButton
app.BurningButton.Text = 'Burning';
app.BurningButton.Position = [11 99 65 22];
app.BurningButton.Value = true;
% Create PoisonButton
app.PoisonButton.Text = 'Poison';
app.PoisonButton.Position = [11 77 65 22];
% Create CorrosionButton
app.CorrosionButton.Text = 'Corrosion';
app.CorrosionButton.Position = [11 55 77 22];
% Create ExplosionButton
app.ExplosionButton.Text = 'Explosion';
app.ExplosionButton.Position = [11 33 76 22];
% Create NoneButton_2
app.NoneButton_2.Text = 'None';
app.NoneButton_2.Position = [11 10 52 22];
% Create CalculateButton
app.CalculateButton = uibutton(app.UIFigure, 'push');
app.CalculateButton.ButtonPushedFcn = createCallbackFcn(app, @CalculateButtonPushed, true);
app.CalculateButton.Position = [118 103 133 26];
app.CalculateButton.Text = 'Calculate';
% Create GurpsSalavgeCharacterDamageCalculatorLabel
app.GurpsSalavgeCharacterDamageCalculatorLabel = uilabel(app.UIFigure);
app.GurpsSalavgeCharacterDamageCalculatorLabel.Position = [65 436 255 22];
app.GurpsSalavgeCharacterDamageCalculatorLabel.Text = 'Gurps/Salavge Character Damage Calculator';
% Create DAMAGEEditFieldLabel
app.DAMAGEEditFieldLabel = uilabel(app.UIFigure);
app.DAMAGEEditFieldLabel.HorizontalAlignment = 'right';
app.DAMAGEEditFieldLabel.Position = [309 105 54 22];
app.DAMAGEEditFieldLabel.Text = 'DAMAGE';
% Create DAMAGEEditField
app.DAMAGEEditField = uieditfield(app.UIFigure, 'numeric');
app.DAMAGEEditField.Position = [378 105 100 22];
% 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 = GurpsDamageAPP
% 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
Matthew Moniz on 7 Apr 2022
The Error is appearing at
app.DAMAGEEditField.Value=str2double(D);
The Player Roll and Damage Resistance values should be numbers (ex Player Roll=15, Enemy DR=12), This should be subtracted to geta a base damage of 3. Then we get to the modifiers, if a weapon has say cutting damage, then the modifier should be 1.5*base damage (rounded down), which would be 4.5 rounded down to 4, if there is effect damage that is just a solid number, so if it has burning damage it should be 5. The calculate buton should display the added result, (15-3)+4+5=12, so calculating should display 12 on the Damage Field.

Matthew Moniz on 8 Apr 2022
I FIGURED IT OUT!!!! on top of adding the code
function CalculateButtonPushed(app, event)
% Create app.MOD and app.EDMG
EffectButtonGroupSelectionChanged(app)
ModifiersButtonGroupSelectionChanged(app)
% Selection changed function: ModifiersButtonGroup
function ModifiersButtonGroupSelectionChanged(app, event)
app.MOD=app.ModifiersButtonGroup.SelectedObject;
to the modifiers button group and
% Selection changed function: EffectButtonGroup
function EffectButtonGroupSelectionChanged(app, event)
app.EDMG=app.EffectButtonGroup.SelectedObject;
selectedButton = app.EffectButtonGroup.SelectedObject;
to the effect button group, so that app.EDMG and app.MOD now have values for the calculation section. Thank you all for your help, I really appreciate it.
##### 2 CommentsShowHide 1 older comment
Matthew Moniz on 8 Apr 2022
I'm not really sure the difference between an object and number in matlab (still very much a novice in terms of matlab), but the app works perfectly now. It displays the correct damage with mods and edmg when the calculate button is pressed.

Matthew Moniz on 7 Apr 2022
Here is my app

Geoff Hayes on 7 Apr 2022
Edited: Geoff Hayes on 7 Apr 2022
@Matthew Moniz - I think one of the problems is with the code here
function CalculateButtonPushed(app, event)
Roll=app.PlayerRollEditField.Value;
DR=app.EnemyDREditField.Value;
Mod=app.MOD;
Edmg=app.EDMG;
D=(Roll-DR)+Mod+Edmg;
app.DAMAGEEditField.Limits=[-1000,1000];
app.DAMAGEEditField.Value=D;
end
. If you place a breakpoint in this code and then press the Caclulate button, you might see (depending upon what you have done) that the Mod and Edmg variables are empty because they haven't been set. Note that the app.MOD or the app.EDMG seem to only be set if a radio button for either the modifers or the effect is changed respectively. I think that you need to assign default values to these two properties when the app is started. And these properties should be initialized given the default modifier or effect.
Note that I also made a correction to the above. Since DAMAGEEditField is a numeric text field, you do not need (or want) to convert this to a string.
##### 2 CommentsShowHide 1 older comment
Geoff Hayes on 7 Apr 2022
The first error makes sense because one or both of Mod or Edmg is empty because the app.MOD or app.EDMG attributes weren't assigned any value when the app is started (and only seem to be assigned a value when the user makes new modifier or effects selection. The second error message makes sense since the code is trying to "put" a string into a numeric text field.

Cris LaPierre on 7 Apr 2022
Another option is to call the other callback functions at the start of the Calculate callback to ensure app.MOD and app.EDMG have been assigned a value.
% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
% Create app.MOD and app.EDMG
EffectButtonGroupSelectionChanged(app)
ModifiersButtonGroupSelectionChanged(app)
Roll=app.PlayerRollEditField.Value;
DR=app.EnemyDREditField.Value;
Mod=app.MOD;
Edmg=app.EDMG;
D=(Roll-DR)+Mod+Edmg;
app.DAMAGEEditField.Limits=[-1000,1000];
app.DAMAGEEditField.Value=D;
end
##### 2 CommentsShowHide 1 older comment
Geoff Hayes on 8 Apr 2022
Perhaps you can repost your code so that we can see the changes.

R2021b

### Community Treasure Hunt

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

Start Hunting!