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

2 ビュー (過去 30 日間)
Dominique
Dominique 2023 年 8 月 3 日
コメント済み: dpb 2023 年 8 月 7 日
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 件のコメント
Dominique
Dominique 2023 年 8 月 7 日
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 2023 年 8 月 7 日
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.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeCategorical Arrays についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by