App Designer add function confused by variable names

3 次查看(过去 30 天)
I've determined the problem and work around for my particular case, and wish to post it here for posterity.
The problem i encountered is this: when adding a new helper fuction in App Designer, the automatic code generation would occasionally place the new function inside of a previous function. This is visually and syntacally terrible, and i'm pretty sure it bricks most code too.
The source of the buggy behavior seems to be with variables whose names end with the word 'end' . I did NOT name any variable 'end', but 'variable_end' is sufficient to cause the problem.
I was able to create a minimal example.
methods (Access = private)
function results = not_buggy_names(app)
variable_named_with_endd = 1;
results = variable_named_with_endd;
end
end
If we have the already existing function not_buggy_names and now press the add function button we correctly get the following behavior
methods (Access = private)
function results = not_buggy_names(app)
variable_named_with_endd = 1;
results = variable_named_with_endd;
end
function results = func2(app)
% this is correctly placed
end
end
However, if instead we have the folowing initial function buggy_names, which contains the string 'end'
methods (Access = private)
function results = buggy_names(app)
variable_named_with_end = 1;
results = variable_named_with_end; % this line is the problem, putting comment here doesn't help
additional_code_doesnt_help = 1;
end
end
and now add a new function yields
methods (Access = private)
function results = buggy_names(app)
variable_named_with_end = 1;
results = variable_named_with_end;% this line is the problem, putting comment here doesn't help
additional_code_doesnt_help = 1;
function results = func2(app)
% The new function got placed within the previous function, but at the end
end
end
end
The variable with 'end' in the name does seem to need to be at the very end of the line.
For example, this code works fine:
methods (Access = private)
function results = fix_buggy_names(app)
variable_named_with_end = 1;
results = variable_named_with_end + 0; % now the line doesn't end with the sting 'end'
end
end
which produces
methods (Access = private)
function results = fix_buggy_names(app)
variable_named_with_end = 1;
results = variable_named_with_end + 0; % now the line doesn't end with the sting 'end'
end
function results = func2(app)
end
end

采纳的回答

Chidvi Modala
Chidvi Modala 2021-3-10
I have brought this issue to the concerned people and it might be fixed in any future release.
  2 个评论
Michael Van de Graaff
Thank you very much.
Hopefully my post here provided sufficient information for others experiencing this problem to find my conclusion and work around.
Michael Van de Graaff
I'd like to furthur note that the same/similar error occurs when I call legend on it's own.
So this breaks:
methods (Access = private)
function results = buggy_ftn(app)
f = figure;
ax = axes(f);
plot(ax,1:15);
legend % if this is commented, no problem!
% legend() works, and legend(ax) works,
function results = func2(app)
% this function was added to the inside of the previous
% function, that's not right!
end
end
end
I just did find and replace legend to legend() and everything was fine.
(although best practice, i assume, would be to pass ax to the legend function)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by