- As a subscript by itself, such as A(5,:) meaning all columns of row 5 of matrix A; or
- in the form START:STOP or START:INCREMENT:STOP
On CodeHS I’m trying to solve 6.1.6, checkerboard, but I keep getting an error of an undefined variable on line 14, I wrote color_value = color_value + 1. Please help me
105 次查看(过去 30 天)
显示 更早的评论
回答(1 个)
Walter Roberson
2022-11-20
There is no MATLAB function named def . You could define such a function and take advantage of command/function duality. For example, (see below for function definition)
def draw_square():
However, there is no way to get past the problem in the next line
for i in range(4):
The : operator can only occur
In all cases, : followed by nothing else is a syntax error.
You cannot use the same technique as I showed for def in order to try to define for as a function that accepts character vectors, because for is a MATLAB keyword
iskeyword()
That is, for is "magic" for MATLAB and cannot be overridden with a function or variable.
Therefore you will need to change your syntax, or else use a different function name instead of for in order to proceed in MATLAB.
function def(varargin)
celldisp(varargin)
end
2 个评论
Walter Roberson
2022-11-21
I already told you, you need to either change to a different function name instead of for so that you can define whatever name as a function that expects character vector inputs, or else you need to fix the syntax for the : operator (after which you would have to worry about what in means in MATLAB.)
def draw_square():
MyFor i in range(4):
function def(varargin)
disp('def here!')
celldisp(varargin)
end
function MyFor(varargin)
disp('MyFor here!')
celldisp(varargin)
end
However you cannot do this with a function named for because for is magic for MATLAB.
Further down in the function you will also have problems with the else: statement, since else is also a reserved word and you have not used a valid colon operation there either.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scope Variables and Generate Names 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!