Bird
Raised Fist0
MATLABdata~10 mins

Structures and field access in MATLAB - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a structure with a field named 'name'.

MATLAB
person.[1] = 'Alice';
Drag options to blanks, or click blank then click option'
Aweight
Bname
Cheight
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using a field name that does not match the value, like 'age' or 'height'.
Forgetting to use the dot (.) to access the field.
2fill in blank
medium

Complete the code to access the 'age' field from the structure 'person'.

MATLAB
ageValue = person.[1];
Drag options to blanks, or click blank then click option'
Aheight
Bweight
Cname
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong field name like 'height' or 'name'.
Missing the dot (.) before the field name.
3fill in blank
hard

Fix the error in the code to correctly assign the field 'score' in the structure 'student'.

MATLAB
student[1]score = 95;
Drag options to blanks, or click blank then click option'
A.
B:
C;
D,
Attempts:
3 left
💡 Hint
Common Mistakes
Using colon (:) or semicolon (;) instead of dot (.) for field access.
Forgetting the dot entirely.
4fill in blank
hard

Fill both blanks to create a structure 'car' with fields 'make' and 'year'.

MATLAB
car.[1] = 'Toyota';
car.[2] = 2020;
Drag options to blanks, or click blank then click option'
Amake
Bmodel
Cyear
Dcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up field names like using 'model' or 'color' instead of 'make' or 'year'.
Forgetting to use the dot (.) before field names.
5fill in blank
hard

Fill all three blanks to create a structure 'book' with fields 'title', 'author', and 'pages'.

MATLAB
book.[1] = '1984';
book.[2] = 'Orwell';
book.[3] = 328;
Drag options to blanks, or click blank then click option'
Aauthor
Bpages
Ctitle
Dpublisher
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect field names like 'publisher' instead of 'author' or 'pages'.
Mixing the order of fields.

Practice

(1/5)
1. What is the correct way to access the field age from a structure person in MATLAB?
easy
A. person['age']
B. person.age
C. person->age
D. person.age()

Solution

  1. Step 1: Understand MATLAB structure field access

    In MATLAB, fields of a structure are accessed using dot notation without parentheses or brackets.
  2. Step 2: Identify correct syntax for field access

    The correct syntax is structure.field, so person.age is correct.
  3. Final Answer:

    person.age -> Option B
  4. Quick Check:

    Use dot notation for fields [OK]
Hint: Use dot notation to access structure fields in MATLAB [OK]
Common Mistakes:
  • Using parentheses like a function call
  • Using brackets like a dictionary
  • Using arrow notation from other languages
2. Which of the following is the correct way to create a structure car with fields make and year in MATLAB?
easy
A. car = struct(make = 'Toyota', year = 2020);
B. car = {make: 'Toyota', year: 2020};
C. car = struct['make' => 'Toyota', 'year' => 2020];
D. car = struct('make', 'Toyota', 'year', 2020);

Solution

  1. Step 1: Recall MATLAB struct creation syntax

    MATLAB uses the struct function with pairs of field names and values as strings and values.
  2. Step 2: Match correct syntax

    car = struct('make', 'Toyota', 'year', 2020); uses struct('make', 'Toyota', 'year', 2020), which is correct MATLAB syntax.
  3. Final Answer:

    car = struct('make', 'Toyota', 'year', 2020); -> Option D
  4. Quick Check:

    Use struct('field', value, ...) to create structures [OK]
Hint: Use struct('field', value) pairs to create structures [OK]
Common Mistakes:
  • Using JSON or dictionary syntax
  • Using equal signs inside struct()
  • Using brackets instead of parentheses
3. What is the output of the following MATLAB code?
person.name = 'Alice';
person.age = 30;
disp(person.age);
medium
A. 'Alice'
B. name
C. 30
D. Error: Undefined variable

Solution

  1. Step 1: Understand structure field assignment

    The code assigns 'Alice' to person.name and 30 to person.age.
  2. Step 2: Identify what disp(person.age) shows

    The disp function prints the value of person.age, which is 30.
  3. Final Answer:

    30 -> Option C
  4. Quick Check:

    disp shows the value of the accessed field [OK]
Hint: disp(struct.field) prints the field's value [OK]
Common Mistakes:
  • Confusing field names with values
  • Expecting the field name instead of value
  • Syntax errors from missing semicolons
4. Consider this MATLAB code snippet:
data = struct('x', 10, 'y', 20);
disp(data.z);
What will happen when this code runs?
medium
A. Error: Reference to non-existent field 'z'.
B. Displays 0
C. Displays empty array []
D. Displays 20

Solution

  1. Step 1: Check structure fields

    The structure data has fields x and y, but no field z.
  2. Step 2: Accessing a non-existent field

    Trying to access data.z causes MATLAB to throw an error about the missing field.
  3. Final Answer:

    Error: Reference to non-existent field 'z'. -> Option A
  4. Quick Check:

    Accessing missing fields causes an error [OK]
Hint: Access only existing fields; missing fields cause errors [OK]
Common Mistakes:
  • Assuming missing fields return 0 or empty
  • Ignoring error messages
  • Confusing field names with variable names
5. You have a structure array students with fields name and score. How do you create a new structure passed containing only students with score >= 50?
hard
A. passed = students([students.score] >= 50);
B. passed = struct('name', students.name, 'score', students.score >= 50);
C. passed = students.score >= 50;
D. passed = students{students.score >= 50};

Solution

  1. Step 1: Understand structure arrays and logical indexing

    In MATLAB, [students.score] creates an array of scores. Logical indexing selects elements where condition is true.
  2. Step 2: Apply logical indexing to filter students

    The expression students([students.score] >= 50) selects only students with scores 50 or above.
  3. Final Answer:

    passed = students([students.score] >= 50); -> Option A
  4. Quick Check:

    Use logical indexing on structure arrays [OK]
Hint: Use logical indexing with [struct.field] to filter arrays [OK]
Common Mistakes:
  • Trying to assign logical array directly
  • Using curly braces instead of parentheses
  • Creating new struct without filtering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard