structSort - %goes from 1 to the length of the names array...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
function [st] = structSort(st2,chara2) %function to sort the structure with respect to the values in the specified %field; if double, then lowest to highest; if char, sort alphabetically; if %the fieldname does not exist, then Invalid Fieldname will return if isfield(st2,chara2) %tests to see if the string is a field in st2 v = st2.(chara2); %finds the first value in st2.chara2 because all of the following values %will also be of that data type v2 = class(v); switch v2 case 'char' vec = {}; %must be a cell of strings in order to use the sort function for x = 1:length(st2)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: %goes from 1 to the length of the names array val = st2(x).(chara2); %finds the values inside the given structure vec = [vec val]; end [sorted pos] = sort(vec); case 'double' vec = ; %must be a vector of doubles to use sort function for x = 1:length(st2) %goes from 1 to the length of the names array val = st2(x).(chara2); %finds the values inside the given structure vec = [vec val]; end [sorted pos] = sort(vec); end st = st2(pos); %indexes the positions of pos i-nto the structure else st = 'Invalid Fieldname'; end e end-...
View Full Document

This note was uploaded on 10/21/2010 for the course CS 1371 taught by Professor Stallworth during the Spring '08 term at Georgia Institute of Technology.

Ask a homework question - tutors are online