Illustration sorting vectors of structures:
This function sorts the structures depend only on the price field. A more common function is shown next, that receives a string which is the name of the field. The function checks first to ensure that the string which is passed is a valid fieldname for the structure. When it is, it sorts depends on that field, and when not it prints an error message and returns an empty vector. The Strings are generated having the name of the vector variable, followed by parentheses having the element number, the period, and lastly the name of the field. The strings are generated by using square brackets to concatenate the pieces of string, and the int2str function is used to convert the element number to a string. Then, by using the eval function, the vector elements are compared to establish the minimum.
Here are some illustrations of calling the function as shown below:
>> pack_by_price = general_pack_sort(packages,'price');