Each function has its own workspace, because this is (one of) the intention(s) of a function. Therefore this line must fail:
save('Variables.m', Total, Individual, defaultval)
It tries to save the variables, whose names are stores as Strings to the variables Total, Individual, and defaultval to the file 'Variables.m'. This contains several problems:
- The variables Total, Individual, and defaultval are not defined in the current workspace, which is the workspace of the subfunction checkbox1_Callback.
- Saving to an M-file is a bad idea, because the resulting file will not have a proper Matlab-Syntax. Use save to write binary files.
- You do not want to write the variable whose name is store in the variable Total, but the variable called 'Total'. Then you need the name as a string:
save('Variables.mat', 'Total')
There are more problems in the posted code: The callbacks of the buttons do not store the values permanently, but only in their current workspace, which vanishes as soon as the function is left. I do not think that close is useful in the buttons callbacks, because it prevents any further activities with the selected values.
Please explain what you want to happen with the variables. "Save in the workspace" requires too much guessing for an answer.
Best Answer