Code passes dictionary first, filename second, which is incorrect.
Final Answer:
Arguments to savemat are in wrong order -> Option C
Quick Check:
savemat(filename, dict) order matters [OK]
Hint: Filename is first argument in savemat [OK]
Common Mistakes:
Swapping filename and data arguments
Assuming lists can't be saved
Forgetting to import savemat
5.
You want to save two variables, a = [1, 2, 3] and b = [[4, 5], [6, 7]], into a MATLAB file vars.mat. Which code correctly saves both variables so MATLAB can load them as a and b?
hard
A. savemat('vars.mat', {'a': a, 'b': b})
B. savemat('vars.mat', {'vars': [a, b]})
C. savemat('vars.mat', {'a': [a], 'b': [b]})
D. savemat('vars.mat', {'a': a}, {'b': b})
Solution
Step 1: Understand savemat input format
savemat requires a dictionary mapping variable names to values.
Step 2: Check how to save multiple variables
Pass a single dictionary with keys 'a' and 'b' mapped to their values.
Step 3: Identify correct option
savemat('vars.mat', {'a': a, 'b': b}) correctly passes {'a': a, 'b': b} as one dictionary.
Final Answer:
savemat('vars.mat', {'a': a, 'b': b}) -> Option A
Quick Check:
Multiple variables saved in one dict [OK]
Hint: Use one dict with all variables as keys [OK]