Error using matlab.ui.​control.Ed​itField/se​t.Value 'Value' must be a character vector or a string scalar.

133 views (last 30 days)
Hi,
I got a question about app deisgner. I am using 2 edit fields (text). The first one is to write a value (e.g : 2). For the second I would like matlab to calculate directly (without action) the multiplication of the first edit field by 3,6. I am using edit field (text) instead of edit field (num) because edit field (text) allow the "Value changing function".
In the last line of the code below, matlab says " Error using matlab.ui.control.EditField/set.Value 'Value' must be a character vector or a string scalar." The first edit field is called "SCx" and the second is "A2".
function SCxEditFieldValueChanging(app, event)
value = event.Value;
value2 = value+3;
app.A2EditField.Value = value2;
end
Need help to understand my mistake or to help me find a solution.
Thanks,
Max-Henri Froger

Accepted Answer

Tommy
Tommy on 24 Apr 2020
value as obtained from event.Value is a character vector, so adding 3 to it does not do what you may think it does:
>> '4'+3
ans =
55
Then, value2 is a double, so you cannot use it to set app.A2EditField.Value, which must be a character vector or string scalar as the error message indicates. Try this:
value2 = num2str(str2double(value)+3);
  3 Comments
Shelynna Adjana Banawe
Shelynna Adjana Banawe on 20 Jun 2023
i've similar issue but i used number field instead of text
a = app.val1.Value;
b = app.val2.Value;
app.results.Value = a+b;
this is the error i got
'Value' must be a character vector or a string scalar.
Steven Lord
Steven Lord on 20 Jun 2023
It seems from the error message that your app.results field is an uieditfield with style equal to "text" whereas your app.val1 and app.val2 are uieditfields with style equal to "numeric". Either make your app.results have style "numeric" or convert the numeric result of a+b into a string before trying to assign it to the Value property of app.results.
n = 42 % numeric result
n = 42
s = string(n) % string result
s = "42"

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!