matlabvalidationuser-interfacedrop-down-menumatlab-app-designer

Numerical values associated with Drop Down options


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

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?


Solution

  • 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.