App Designer app property is saved as double although I enter strings

9 views (last 30 days)
In App Designer I run into the issue of not being able to assign a string (or any other variable type) other than double to new properties I create.
This is my code (simplified):
properties (Access = public)
nameSave
end
% Button pushed function: Calculate
function CalculateButtonPushed(app, event)
app.nameSave(1,1) = "John"
app.nameSave(1,2) = "Peter"
end
I would now assume that nameSave is a 1x2 string array just like how it works in the normal editor. However, in App Designer, nameSave is a 1x2 double filled with [NaN,NaN] although I obviously assign strings to nameSave.
What is the mistake I am making?

Accepted Answer

Adam Danz
Adam Danz on 8 Feb 2020
Edited: Adam Danz on 8 Feb 2020
By default the property is initially defined to class=double.
Here are two solutions.
Set the property to a string array by placing the word string next to the property definition.
properties (Access = private)
nameSave string
Don't use indexing on the first assignment
app.nameSave = "John"; %now it's a string array!
app.nameSave(2) = "Mary";
If needed, you can use the following to determine whether nameSave is emtpy: isempty(app.nameSave)
  2 Comments
hzh
hzh on 31 Aug 2021
properties (Access = private)
nameSave string
This is really the worst syntax I have ever seen in my life. Took me an hour to figure out how to properly declare the property to be a string array. Very counterintuitive.
properties (Access = private)
nameSave = ""
should have been the one used
Adam Danz
Adam Danz on 31 Aug 2021
Edited: Adam Danz on 31 Aug 2021
> This is really the worst syntax I have ever seen in my life
I doubt it, but I feel your frustration.
> Took me an hour to figure out how to properly declare the property to be a string array
I'm sorry this answer didn't perfectly address whatever you were looking for and that you had to invest some of your own valuable time to figure out a solution that makes sense to you.
I took some time to find this page below so you can get more familiar with the available syntaxes for declaring properties.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!