Getting wrong sizes for string array. (in App Designer)
이전 댓글 표시
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)
% 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)
% case 3:
k = app.col + 1;
app.array_test = strings([100, k]); % creates size of (100,5) as expected
size(app.array_test)
Why Case 2 does not add new column dynamically in App Designer ?
Thanks.
댓글 수: 6
Jon
2023년 8월 3일
Could you please provide a small, example app designer, app, that reproduces the problem
Cris LaPierre
2023년 8월 3일
I created a simple app in App Designer that just uses the code you have shared, and the app works as expected in R2023a, meaning it returns 100x5 for case 1 and 100x6 for both case 2 and 3.
If you are still seeing the issue, try restarting MATLAB. If the issue continues, I suspect there is other code affecting the result that has not been shared.
Specifically that app.col has been redefined behind the scenes at the time of the call. One way this could be happening is if the acutal code defines app.col as something like
app.col=length(x);
where x is an input array and now the shape has changed and instead of the number columns returned as the variable name suggests, the shape has changed and it now returns the other dimension if went from 4x5 to 4x3, say. With event-driven coding, such things are easy to have occur, and since the app struct is global, changes from afar have app-wide consequences.
Mann Baidi
2023년 8월 7일
Hi,
I tried executing your code on my local desktop, it returned 100x5, 100x6 and 100x6 for the case 1,2 &3 respectively. I sugget you to look through your code and check if the value of app.col is changing anywhere.
Dominique
2023년 8월 7일
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개)
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!