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

102 views (last 30 days)

Answers (1)

Walter Roberson
Walter Roberson on 20 Nov 2022
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():
varargin{1} = 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
  1. As a subscript by itself, such as A(5,:) meaning all columns of row 5 of matrix A; or
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
ans = 20×1 cell array
{'break' } {'case' } {'catch' } {'classdef' } {'continue' } {'else' } {'elseif' } {'end' } {'for' } {'function' } {'global' } {'if' } {'otherwise' } {'parfor' } {'persistent'} {'return' } {'spmd' } {'switch' } {'try' } {'while' }
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)
Walter Roberson
Walter Roberson on 21 Nov 2022
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():
def here! varargin{1} = draw_square():
MyFor i in range(4):
MyFor here! varargin{1} = i varargin{2} = in varargin{3} = range(4):
function def(varargin)
disp('def here!')
function MyFor(varargin)
disp('MyFor here!')
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.

Sign in to comment.


Find more on Characters and Strings in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by