2

So I am creating an app to work out a value based on a series of variables. The variables are:

  • Gender
  • Age
  • Weight
  • Creatinine

Here's what the app looks like:

Showing the UI off

In order to simplify the process somewhat I decided to make the gender selection a dropdown menu, this has caused me some issues since I have it setup like so:

Drop Down properties

And the maths associated with the button looks like so:

  function CalculateButtonPushed(app, event)
            gender = app.PatientGenderDropDown.Value ;
            age = app.PatientAgeEditField.Value ;
            weight = app.LeanBodyWeightEditField.Value ;
            serum = app.SerumCreatinineEditField.Value ;
            final = (gender*(age)*weight) / (serum) ;
            app.ResultEditField.Value = final ;
        end
    end

Running this gives the following error:

Error using matlab.ui.control.internal.model.AbstractNumericComponent/set.Value (line 104) 'Value' must be numeric, such as 10.

As far as I am aware, the values I input into ItemsData are numeric values. Have I missed something or is there a better way to do this?

monotiller
  • 37
  • 1
  • 4
  • 9
  • How are you initializing your variables? javascript figures out it's type the first time it's assigned a value so var gender = "" would make gender a string. var gender = 0 would make gender an int. – Mike Jul 15 '17 at 21:39

1 Answers1

1

If you put a breakpoint in the offending file on the appropriate line (by running the below code),

dbstop in uicomponents\+matlab\+ui\+control\+internal\+model\AbstractNumericComponent.m at 87

you could see the following in your workspace, after clicking the button:

workspace snapshot

There are two separate problems here, both of which can be identified by looking at the newValue validation code (appearing in AbstractNumericComponent.m):

% newValue should be a numeric value.
% NaN, Inf, empty are not accepted
validateattributes(...
    newValue, ...
    {'numeric'}, ...
    {'scalar', 'real', 'nonempty'} ...
    );

Here are the issues:

  1. The new value is a vector of NaN.
    The reason for this is in this line:

    final = (gender*(age)*weight) / (serum) ;
    

    where serum has a value of 0 - so this is the first thing you should take care of.

  2. The new value is a vector of NaN.
    This is a separate problem, since the set.Value function (which is implicitly called when you assign something into the Value field), is expecting a scalar. This happens because gender is a 1x4 char array - so it's treated as 4 separate numbers (i.e. the assumption about ItemsData being a numeric is incorrect). The simplest solution in this case would be to str2double it before use. Alternatively, store the data in another location (such as a private attribute of the figure), making sure it's numeric.

Graham
  • 7,431
  • 18
  • 59
  • 84
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • Thank you so much for this. Although I didn't use your method specifically I did find that converting the number to a double somewhere else worked. Thank you again for your help! – monotiller Jul 16 '17 at 17:00