2

I am using AppDesigner and Matlab R2017B.

I was wondering how I would go about changing number format in matlab. To make myself more clear: I have 3 EditFileds , the user enters a number into two of the fields and presses a claculate button which adds the 2 values and outputs the answer into the third EditField. This works all fine. However, if the number output is larger than 9999 then I get horrible exponent form like 1.0e+04 how can I tell matlab to produce more place values? E.g: instead of 1.0e+4 I get 10,000

DaveSwans
  • 57
  • 1
  • 2
  • 10

2 Answers2

1

You can change the display either manually or programatically

Manually: select the EditField, go to 'Design View' and change its 'DISPLAY' property.

Programatically:

fig = uifigure;
edt = uieditfield(fig,'numeric','ValueDisplayFormat','%.2f');

where %.2f is a format operator that forces 2 decimal places.

Also, see numeric uieditfield properties

0

If you want to have absolute control over the display format for the "third EditField", then you may consider changing the type from NumericEditField to EditField, i.e. the string version. Then you can format the number as a string and display as you want, e.g. with units or comma separators.

Using your example:

"Instead of 1.0e+4 I [want] 10,000".

Having an app in appdesigner with the following graphics objects:

% Properties that correspond to app components
properties (Access = public)
    UIFigure              matlab.ui.Figure
    LabelNumber1          matlab.ui.control.Label
    Number1               matlab.ui.control.NumericEditField
    LabelNumber2          matlab.ui.control.Label
    Number2               matlab.ui.control.NumericEditField
    ResultEditFieldLabel  matlab.ui.control.Label
    Result                matlab.ui.control.EditField
    Calculate             matlab.ui.control.Button
end

Notice the Result object is a standard string-based EditFIeld, your button callback function could be:

% Button pushed function: Calculate
function doCalculation(app, event)
    value_1 = app.Number1.Value;
    value_2 = app.Number2.Value;
    % Calculation
    result = value_1 + value_2;
    resultFormatted = num2str(result);
    app.Result.Value = resultFormatted;
end

But to get the formatting like you ask, i.e. 10,000, you'll need to format the resultFormatted string accordingly. Now, having the result EditField as a string, any formatting changes you make will be preserved, such as the comma separator. It also allows the flexibility to avoid having decimals show when there are none (10000 => '%.2f' => '10000.00'), or unwanted rounding (56.576 => '%.2f' => '56.58').

Getting the comma-separator in the formatting is not explicitly part of your question, but there are many ways to do that. I'm happy to share my solution should anyone want it.

Best Regards.

Khlick
  • 260
  • 1
  • 9