Task: Use the save command to save the Norfolk temperature variables that you created in the previous task. Clear these variables from your workspace. Write a script to read the .mat file to recover the variables, plot the temperature vs time, and add the line showing the mean value.
MATLAB has a mechanism to save variables in a binary file format (which means that it is not human readable with an editor). The basic command is save('FILENAME') which will create a file named FILENAME.mat which will contain all variables and values in the current workspace.
You can recover all of the variables and values with the command load('FILENAME'). Notice that this command does not just retrieve all of the numbers, but it also saves the name of the variables. This is a convenient way to save your place in a project that you want to return to later if you must exit MATLAB.
Be aware that the load('FILENAME.mat') recovers the variables and values. Don't use data=load('FILENAME.mat'), as we did in the previous task to read columns of numbers. If you do this, the variables in the .mat file will be saved to a structure that begind with the name "data". A variable in the .mat file called "temp" will be vailable as "data.temp". We will see more about structure variables later.
A more practical use of these commands is to save values (data) that you may want to use later. For example, in the previous task, you read a data file, created variables year, month, temp, time, MeanTemp, AnomalyTemp. To avoid repeating the work you did to create these variables, you could save them to a MATLAB file with
save('NorfolkTemp.mat','year','month','temp','time',... 'MeanTemp','AnomalyTemp');which saves the indicated variables to the file. The first string in the command is the file name. The remaining strings are the names of the variables to write to the file.
There is no restriction on the length of a MATLAB command, but it is sometimes necessary to continue a command over more than one line. If for no other reason than it is easier to read. Simply put an ellipsis ("..." three periods) at the end of the line, and continue on the next line. There can be any number of line continuations.
The command load('NorfolkTemp') will recover all of the variables in the file. In some cases, you may want some, but not all, variables in the file. In that case, the command might be load('NorfolkTemp','time','temp'), which recovers only the two indicated variables.
Flow chart to complete the task.
%%% Task 4.2 %%% continue from task 4.1 %%% save variables to .mat file %%% clear variables %%% load variables %%% plot temperature %%% add labels and title
Script to complete this task:
%%% Task 4.2 %%% continue from task 4.1 %%% save variables to .mat file save('NorfolkTemp.mat','time','temp','year','month','MeanTemp'); %%% clear variables clear time temp year month MeanTemp %%% load variables load('NorfolkTemp.mat','time','temp') %%% plot temperature figure plot(time,temp,'b') %%% add labels and title title('Norfolk Monthly Mean Temp') xlabel('time(yr)') ylabel('deg F')