App Designer add function confused by variable names

6 views (last 30 days)
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

Accepted Answer

Chidvi Modala
Chidvi Modala on 10 Mar 2021
I have brought this issue to the concerned people and it might be fixed in any future release.
  2 Comments
Michael Van de Graaff
Michael Van de Graaff on 3 Dec 2021
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)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!