Join Patrick Royal for an in-depth discussion in this video Editing variables manually, part of Learning MATLAB.
Writing, debugging, and polishing scripts to carry out any necessary calculations, is one of the primary uses of Matlab, but it's not the only one. Sometimes there might a single problem, that only has to be solved, one time in one specific way, and it wouldn't make sense to write and test the script line by line to solve it. In these situations it's worthwhile to understand how to manually edit and manipulate variables from within the command window. In this video I'll use the daily data.csv and building .jpg files as examples. So let's import those now (BLANK_AUDIO).
We will end up with 4 different variables. 3, 2,515 by 1 vectors containing closed prices, volumes and dates for the S&P 500. And 1, 685 by 1,024 by 3 matrix Containing RGB values for each of the pixels in the building image. The easiest way to edit a variable directly is to double click on it. Double clicking on any of the stock variables will bring up a table containing all of their values in a list. For a matrices with more than one column, the additional columns will be displayed here as well. From within this view, you can edit any value in the matrix simply by double clicking on it.
Changing the number stored in the cell and then pressing enter. This is an especially effective editing method for simple variables such as scalars and small vectors. Obviously for large matrices, writing an algorithm will be much faster than manually changing a variable. Now let's try to edit the image matrix. When this variable is opened, Matlab will not display the data. Instead it will display an error message saying that the variable size is too large to be summarized.
The variable view will also not allow editing if the variable is more than 2 dimensional. Generally this isn't a problem because it's not practical to edit a variable with more than 3 dimensions or 524,288 elements manually. However, if you know that you need to edit only a certain element, you can edit just that element by typing tempvar equals the variable name, x, y, z, and so on, depending on the dimensions. The x, y, and z values are the indices of the matrix, and they can be either scalars, if you want a single value, or ranges of numbers if you want a collection of values.
You can then edit tempvar in the editor window as normal and return its values to the original variable by reversing the equation and typing the variable name of those same indices or ranges equal tempvar. For instance, if I wanted to chance just the blue values in the center of the building image, I could type tempvar equals building of 300-350, 500-550, 3. And then double click on temp far to open it and edit it those values manually.
For this example the change I'll make is that temp far equals 0's of 51,51. So we just set all of the values to zero. I can now reassign these values back to the building matrix by reversing the earlier command and typing building of 300 to 350, 500 to 550, 3 equals tempvar. This would be a good example of a place where you might want to use a semicolon, as you can see that forcing all of these values to display in the command window takes a lot of extra time.
When the matrix is displayed as an image once more by typing Image of building. It's easy to see the change. The main advantage of a script over command lines in MATLAB is that it can be saved and run again later. However, it is possible to save the states of the variables that you're working on even without a script. Click on Save Workspace in the Variable tab and choose the names for your workspace. This will save a record of the exact values of all of your variables at this point as .mat file. Double clicking on the file later on will then recreate those variables with exactly the same values.
It will not change or delete any other variables in the workspace, but it will override any new values to which your saved variables had been assigned. With these tools, MATLAB makes it easy to work through a problem quickly and efficiently without worrying about writing a permanent script or a function.
- Installing MATLAB
- Working with MATLAB variables
- Working with matrix and scalar operations
- Creating functions
- Understanding performance considerations
- Building basic plots
- Creating responsive programs
- Editing variables manually
- Working with the Statistics Toolbox