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

18 次查看(过去 30 天)
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?

采纳的回答

Adam Danz
Adam Danz 2020-2-8
编辑:Adam Danz 2020-2-8
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 个评论
hzh
hzh 2021-8-31
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 2021-8-31
编辑:Adam Danz 2021-8-31
> 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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by