Getting wrong sizes for string array. (in App Designer)

4 views (last 30 days)
I'm coding in App Designer.
Any body knows why the following snippets return different array sizes?
app.col = 5; % array column
% case 1:
app.array_test = strings([100, app.col]); % intention of preallocating an array of size (100,5)
size(app.array_test)
ans = 1×2
100 5
% case 2: here is a problem.
app.array_test = strings([100, app.col + 1 ]); % in App Designer, it still creates size of (100,5). Not (100,6)
size(app.array_test)
ans = 1×2
100 6
% case 3:
k = app.col + 1;
app.array_test = strings([100, k]); % creates size of (100,5) as expected
size(app.array_test)
ans = 1×2
100 6
Why Case 2 does not add new column dynamically in App Designer ?
Thanks.
  6 Comments
Dominique
Dominique on 7 Aug 2023
Thank you guys @Jon, @dpb and @Mannan Baidi
I couldn't reproduce the error in a seperate app.
But I've used length() several times. So I think @dpb is right. Something must have occured when I called length().
Thanks for checking this question.
I will close this.
dpb
dpb on 7 Aug 2023
length is a very dangerous function in the way most folks use it; if it hadn't been introduced by Cleve originally, it probably wouldn't be defined now as it is; at least with no checking/caveats as it is currently.
Wouldn't be the first to have been "bit" by that behavior. It's not the only way such could happen, of course, but it's one of the more likely ways to get an unexpected result silently.
If it is supposed to be a constant, you could set a dbstop condition if/when the value weren't; unfortunately, I don't think there is a way to set a "if changed" breakpoint explicitly; you have to be able to write a logical expression.

Sign in to comment.

Answers (0)

Categories

Find more on Categorical Arrays in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!