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

조회 수: 20 (최근 30일)
Matthew Moniz 2022년 4월 7일
댓글: Matthew Moniz 2022년 4월 8일
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
##### 댓글 수: 3이전 댓글 1개 표시이전 댓글 1개 숨기기
Geoff Hayes 2022년 4월 7일
@Matthew Moniz - which line of code is generating the error? I see that you have
app.MOD =floor(0.5*(app.PlayerRollEditField.Value-app.EnemyDREditField.Value))
Do you need to convert (for example) app.PlayerRollEditField.Value to a number assuming that it is a string? Do you need to do this for all edit fields?
Matthew Moniz 2022년 4월 7일
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 2022년 4월 8일
I FIGURED IT OUT!!!! on top of adding the code
function CalculateButtonPushed(app, event)
% Create app.MOD and app.EDMG
EffectButtonGroupSelectionChanged(app)
ModifiersButtonGroupSelectionChanged(app)
to my calculate button section, I also had to add
% 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없음 표시없음 숨기기
Geoff Hayes 2022년 4월 8일
I don't think adding lines like
app.MOD=app.ModifiersButtonGroup.SelectedObject;
or
app.EDMG=app.EffectButtonGroup.SelectedObject;
is correct since you are initializing these properties (MOD or EDMG) with an object and not number. More likely the fix is just calling the methods to ensure that the MOD or EDMG is set before you try to use them.
Matthew Moniz 2022년 4월 8일
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.

댓글을 달려면 로그인하십시오.

### 추가 답변 (3개)

Matthew Moniz 2022년 4월 7일
Here is my app
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

Geoff Hayes 2022년 4월 7일
편집: Geoff Hayes 2022년 4월 7일
@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없음 표시없음 숨기기
Matthew Moniz 2022년 4월 7일
That was how I originally had the DAMGEEditFIeld, but Matlab threw the error 'Value' must be double scalar, when I added str2double to the code, it threw the error 'Value' must be a double scalar within the range of 'Limits'.
Geoff Hayes 2022년 4월 7일
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 2022년 4월 7일
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없음 표시없음 숨기기
Matthew Moniz 2022년 4월 8일
I did this and I'm still getting the same errors
Geoff Hayes 2022년 4월 8일
Perhaps you can repost your code so that we can see the changes.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by