An important part of electrical engineering is PCB design On
An important part of electrical engineering is PCB design. One important part of PCB design when using microcontrollers is determining which pin package to use. Develop a MATLAB function that requests input on whether to use an LQFP100, LQFP144, or LQFP176 pin package and outputs the corresponding pin package. The MATLAB function should receive an input of 1, 2, or 3 to determine the respective pin package. Think of it as a label maker with a dial input. It should be made sure that the full strings are returned and outputted to the console. Use a method other than the switch/case or if/else structures in order to develop this solution (i.e. build a matrix). Answer the following questions:
1) In this context, explain the terminology ‘map’.
2) What is a good way of storing such a map in MATLAB?
3) How would you change the function to use 0, 1, and 2 as inputs instead of 1, 2, and 3?
4) How does MATLAB differ from Python when using arrays / lists?
Solution
prompt = \'Enter values 1 for LQFP100 2 for LQFP144 and 3 for LQFP176 \';
c = {\'LQFP100\', \'LQFP144\', \'LQFP176\'};
x = input(prompt)
c(1,x)
A Map object is a data structure that allows you to retrieve values using a corresponding key. Keys can be real numbers or character vectors and provide more flexibility for data access than array indices, which must be positive integers. Values can be scalar or nonscalar arrays.
Construction
mapObj = containers.Map constructs an empty Map container mapObj.
mapObj = containers.Map(keySet,valueSet) constructs a Map that contains one or more values and a unique key for each value.
mapObj = containers.Map(keySet,valueSet,\'UniformValues\',isUniform) specifies whether all values must be uniform (either all scalars of the same data type, or all character vectors). Possible values for isUniform are logical true (1) or false (0).
mapObj = containers.Map(\'KeyType\',kType,\'ValueType\',vType) constructs an empty Map object and sets the KeyType and ValueType properties. The order of the key type and value type argument pairs is not important, but both pairs are required.
Input Arguments
keySet
1-by-n array that specifies n unique keys for the map.
All keys in a Map object are real numeric values or all keys are character vectors. If n > 1 and the keys are character vectors, keySet must be a cell array. The number of keys in keySet must equal the number of values in valueSet.
valueSet
1-by-n array of any class that specifies n values for the map. The number of values in valueSet must equal the number of keys in keySet.
\'UniformValues\'
Parameter character vector to use with the isUniform argument.
isUniform
Logical value that specifies whether all values are uniform. If isUniform is true (1), all values must be scalars of the same data type, or all values must be character vectors. If isUniform is false (0), then containers.Map sets the ValueType to \'any\'.
Default: true for empty Map objects, otherwise determined by the data types of values in valueSet.
\'KeyType\'
Parameter character vector to use with the kType argument.
kType
Character vector that specifies the data type for the keys. Possible values are \'char\', \'double\', \'single\', \'int32\', \'uint32\', \'int64\', or \'uint64\'.
Default: \'char\' for empty Map objects, otherwise determined by the data types of keys in keySet. If you specify keys of different numeric types, kType is \'double\'.
\'ValueType\'
Literal character vector parameter to use with the vType argument.
vType
Character vector that specifies the data type for the values. Possible values are \'any\', \'char\', \'logical\', \'double\', \'single\', \'int8\', \'uint8\', \'int16\', \'uint16\', \'int32\', \'uint32\', \'int64\', or \'uint64\'.
Default: \'any\' when you create an empty Map object or when you specify values of different sizes or types, otherwise determined by the data type of valueSet.
Properties
Count
Unsigned 64-bit integer that represents the total number of key-value pairs contained in the Map object. Read only.
KeyType
Character array that indicates the data type of all keys in the Map object. The default KeyType for empty Map objects is \'char\'. Otherwise, KeyType is determined from the data type of the keySet inputs. Read only.
ValueType
Character array that indicates the data type of all values in the Map object. If you construct an empty Map object or specify values with different data types, then the value of ValueType is \'any\'. Otherwise, ValueType is determined from the data type of the valueSet inputs. Read only.
Methods
isKey Determine if containers.Map object contains key
keys Identify keys of containers.Map object
length Length of containers.Map object
remove Remove key-value pairs from containers.Map object
size Size of containers.Map object
values Identify values in containers.Map object
Construct a Map and View Properties
Construct a Map object that contains rainfall data for several months:
keySet = {\'Jan\', \'Feb\', \'Mar\', \'Apr\'};
valueSet = [327.2, 368.2, 197.6, 178.4];
mapObj = containers.Map(keySet,valueSet)
This code returns a description of the map, including the property values:
mapObj =
Map with properties:
Count: 4
KeyType: char
ValueType: double
Get a specific property using dot notation, such as
mapObj.Count
which returns
ans =
4
Look Up Values in a Map
Use the map created in the previous example to find the rainfall data for February:
rainFeb = mapObj(\'Feb\')
This code returns
rainFeb =
368.2000
Add a Single Value and Key to a Map
Add data for the month of May to the map created in the first example:
mapObj(\'May\') = 100.0;
Add Multiple Values and Keys by Concatenating Maps
Create a map that contains rainfall data for June, July, and August, and add the data to mapObj (from previous examples):
keySet = {\'Jun\',\'Jul\',\'Aug\'};
valueSet = [ 69.9, 32.3, 37.3];
newMap = containers.Map(keySet,valueSet);
mapObj = [mapObj; newMap];
Map objects only support vertical concatenation (that is, adding columns with a semicolon, ;). When concatenating maps, the data type of all values must be consistent with the ValueType of the leftmost map. In this example, both maps have the a ValueType of double.
Get the Keys or Values in a Map
Determine all the keys of mapObj (from previous examples) by calling the keys method:
allKeys = keys(mapObj)
This method returns the keys in alphabetical order:
allKeys =
\'Apr\' \'Aug\' \'Feb\' \'Jan\' \'Jul\' \'Jun\' \'Mar\' \'May\'
Get multiple values from the map by calling the values method. Like the keys method, you can request all values with the syntax values(mapObj). Alternatively, request values for specific keys. For example, view the values for March, April, and May in mapObj:
springValues = values(mapObj,{\'Mar\',\'Apr\',\'May\'})
This method returns the values in a cell array, in the order corresponding to the specified keys:
springValues =
[197.6000] [178.4000] [100]
Remove Keys and Values
Remove the data for March and April from mapObj (from previous examples) by calling the remove method, and view the remaining keys:
remove(mapObj,{\'Mar\',\'Apr\'});
keys(mapObj)
This code returns
ans =
\'Aug\' \'Feb\' \'Jan\' \'Jul\' \'Jun\' \'May\'
Create a Map with Nonscalar Values
Map integer keys to nonscalar arrays, and view the value for one of the keys:
keySet = [5,10,15];
valueSet = {magic(5),magic(10),magic(15)};
mapObj = containers.Map(keySet,valueSet);
mapObj(5)
This code returns
ans =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Construct an Empty Map
Construct a map with no values, but set the KeyType and ValueType properties:
mapObj = containers.Map(\'KeyType\',\'char\',\'ValueType\',\'int32\')
This code returns
mapObj =
Map with properties:
Count: 0
KeyType: char
ValueType: int32
Specify Whether Values Are Uniform
Construct a map with numeric values, and specify that the values do not have to be uniform:
keySet = {\'a\',\'b\',\'c\'};
valueSet = {1,2,3};
mapObj = containers.Map(keySet,valueSet,\'UniformValues\',false);
This map allows nonnumeric values, so
mapObj(\'d\') = \'OK\';
values(mapObj)
returns
ans =
[1] [2] [3] \'OK\'
\\
In MATLAB®, the basic data type is a multidimensional array of double precision floating point numbers. Most expressions take such arrays and return such arrays. Operations on the 2-D instances of these arrays are designed to act more or less like matrix operations in linear algebra.
In NumPy the basic type is a multidimensional array. Operations on these arrays in all dimensionalities including 2D are elementwise operations. However, there is a special matrix type for doing linear algebra, which is just a subclass of the array class. Operations on matrix-class arrays are linear algebra operations.
MATLAB® uses 1 (one) based indexing. The initial element of a sequence is found using a(1). See note \'INDEXING\'
Python uses 0 (zero) based indexing. The initial element of a sequence is found using a[0].
MATLAB®\'s scripting language was created for doing linear algebra. The syntax for basic matrix operations is nice and clean, but the API for adding GUIs and making full-fledged applications is more or less an afterthought.
NumPy is based on Python, which was designed from the outset to be an excellent general-purpose programming language. While Matlab\'s syntax for some array manipulations is more compact than NumPy\'s, NumPy (by virtue of being an add-on to Python) can do many things that Matlab just cannot, for instance subclassing the main array type to do both array and matrix math cleanly.
In MATLAB®, arrays have pass-by-value semantics, with a lazy copy-on-write scheme to prevent actually creating copies until they are actually needed. Slice operations copy parts of the array.
In NumPy arrays have pass-by-reference semantics. Slice operations are views into an array.
In MATLAB®, every function must be in a file of the same name, and you can\'t define local functions in an ordinary script file or at the command-prompt (inlines are not real functions but macros, like in C).
NumPy code is Python code, so it has no such restrictions. You can define functions wherever you like.
MATLAB® has an active community and there is lots of code available for free. But the vitality of the community is limited by MATLAB®\'s cost; your MATLAB® programs can be run by only a few.
!NumPy/!SciPy also has an active community, based right here on this web site! It is smaller, but it is growing very quickly. In contrast, Python programs can be redistributed and used freely. See Topical_Software for a listing of free add-on application software, Mailing_Lists for discussions, and the rest of this web site for additional community contributions. We encourage your participation!
MATLAB® has an extensive set of optional, domain-specific add-ons (\'toolboxes\') available for purchase, such as for signal processing, optimization, control systems, and the whole SimuLink® system for graphically creating dynamical system models.
There\'s no direct equivalent of this in the free software world currently, in terms of range and depth of the add-ons. However the list in Topical_Software certainly shows a growing trend in that direction.
MATLAB® has a sophisticated 2-d and 3-d plotting system, with user interface widgets.
Addon software can be used with Numpy to make comparable plots to MATLAB®. Matplotlib is a mature 2-d plotting library that emulates the MATLAB® interface. PyQwt allows more robust and faster user interfaces than MATLAB®. And mlab, a \"matlab-like\" API based on Mayavi2, for 3D plotting of Numpy arrays. See the Topical_Software page for more options, links, and details. There is, however, no definitive, all-in-one, easy-to-use, built-in plotting solution for 2-d and 3-d. This is an area where Numpy/Scipy could use some work.
MATLAB® provides a full development environment with command interaction window, integrated editor, and debugger.
Numpy does not have one standard IDE. However, the IPython environment provides a sophisticated command prompt with full completion, help, and debugging support, and interfaces with the Matplotlib library for plotting and the Emacs/XEmacs editors.
MATLAB® itself costs thousands of dollars if you\'re not a student. The source code to the main package is not available to ordinary users. You can neither isolate nor fix bugs and performance issues yourself, nor can you directly influence the direction of future development. (If you are really set on Matlab-like syntax, however, there is Octave, another numerical computing environment that allows the use of most Matlab syntax without modification.)
NumPy and SciPy are free (both beer and speech), whoever you are.





