The error message "Unable to write file /Testing: Read-only file system" suggests that the script is trying to save the file to the root directory (/) of your macOS filesystem, which is typically protected and not writable by user-level processes. This could be due to an incorrect path specified in the variables dlocs{ii} and dnames{ii}.
Here are some ways to resolve the issue:
1. Check the Path Variables
The variables "dlocs{ii}" and "dnames{ii}" determine where the “save” function attempts to write the file. You need to ensure that dlocs{ii} points to a directory where you have write access. Here's how you can check and correct the path:
Print the Path: Add a line of code before the save command to print the full path to the console. This will help you verify the path being used.
fullPath = fullfile(dlocs{ii}, dnames{ii});
disp(['Attempting to save to: ', fullPath]);
Validate the Path: If the printed path is not what you expect, or if it's pointing to the root directory /, you need to adjust dlocs{ii} to point to a valid, writable directory.
2. Correct Directory Permissions
Even if the path is correct, the directory might not have the necessary write permissions. Here's how to check and modify permissions:
Using Finder: Navigate to the directory in Finder, right-click on it, and select 'Get Info'. Scroll down to 'Sharing & Permissions' and ensure that your user has 'Read & Write' access. If not, click the lock icon to make changes, and then set the appropriate permissions.
Using Terminal: Open the Terminal and use the "chmod" command to grant write access to your user. For example, to grant write access to the current user for a folder named 'MyFolder', you would use in bash:
chmod u+w /path_to_MyFolder
By following these detailed steps, you should be able to possibly diagnose and fix the issue with the save function in MATLAB. If the problem persists, double-check the values of "dlocs{ii}" and "dnames{ii}" at runtime, and ensure that your script has the necessary permissions to write to the desired location.