Examples

  1. Assign numeric values to each of the following variable names:
    • wallHeight
    • 1stChoice
    • sumOfAll
        >> wallHeight = 4.566
        
        wallHeight =
        
            4.5660
        
        >> 1stChoice = 3
        ??? 1stChoice = 3
             |
        Error: Unexpected MATLAB expression.
        
        >> sumOfAll = 1 + 2 + 3 + 4 + 5
        
        sumOfAll =
        
            15
        
        >> 
        

    Notice the error that occurs when you start a variable name with a digit. This is not allowed in Matlab.

  2. Enter the following at the Command Window. What happens? Why?
    >> q+3 = p
        >> q+3 = p
        ??? q+3 = p
            |
        Error: The expression to the left of the equals sign is not a valid target for an assignment.
    
        >>     
        

    This error is generated because it doesn't make sense to assign a value ( p ) to an expression ( q+3 ). You can only assign values to single variable names.

  3. What happens when you enter a value like 2,929 as 2,929?

    Try this!

    A = 2,929
    	>> A = 2,929
    
    	A =
    
    		2
    
    
    	ans =
    
    	929
    
    	>> 

    Carefully view the output produced in the Command Window to see that two variables were given values. A gets the value 2 and ans gets the value 929. What if you used two commas?

  4. Assign the value 70 to the variable F and write a statement that evaluates this formula and assigns the result to the variable C. ""
    	>> F = 70
    
    	F =
    
    		70
    
    	>> C = (F-32)*5/9
    
    	C =
    
    	    21.1111
    
    	>>  

    The formula given converts a Fahrenheit temperature into the equivalent Celsius temperature.

  5. Reassign F to the value 212. Check the Workspace window to see if the value of C changed. Why did or didn't the value of C change?

    The value of C does not change. You would have to also reexecute the C = (F-32)*5/9 statement.