How can you change the order of the values of every field in a structure without knowing the amount of fields? (MatLab) -


write function called sortstruct takes in structure array , sorts structure array values in inputted field. if values numeric, function sorts them lowest highest. if values characters, function sorts words alphabetically case mattering (e.g. 'apples' before 'apples'). if field not exist in structure array, function returns string 'invalid field name'.

here have far:

function [ structsort ] = sortstruct( strucarray, fname )  if isfield(strucarray, fname) ~= '1'     structsort = 'invalid field name'; end  = class(fname);  = 'double'     [sorteddoub inddoub] = sort(fname);     fieldn = fieldnames(strucarray);     num = length(fieldn);     strucarray = setfield(strucarray, fname, sorteddoub);      structsort = setfield(strucarray, fieldn, fieldn(inddoub)); end  = 'char'     [sortedchar indchar] = sort(char2num(fname(1))); end 

you seem on right track, aside syntax errors. few comments:

  • isfield returns true/false
  • a better way test data type of variables using is** family of functions: isnumeric, isstruct, ischar, ...
  • you should read on difference between if/for/... constructs
  • the sort function can handle both vector of numbers , cell array of strings. should use functionality (read documentation page first)
  • the syntax accessing structure fields dynamically is: structname.(dynamicexpression)

having said that, here how write such function:

function structsorted = sortstruct(structarray, fname)     if ~isfield(structarray,fname)         error('invalid field name')     end      if isnumeric( structarray(1).(fname) )         data = [structarray.(fname)];     else         data = {structarray.(fname)};     end      [~,order] = sort(data);     structsorted = structarray(order); end 

and lets test function random array of structures:

%# lets build array of structures chars = char(97:122); str = cellstr( chars(ceil(numel(chars).*rand(10,6))) ); [s(1:10).str] = str{:}; num = num2cell(rand(10,1)); [s(1:10).num] = num{:};  %# sort according field s_str = sortstruct(s, 'str'); s_num = sortstruct(s, 'num'); %#s_err = sortstruct(s, 'zzzzz');  %# compare 2 sorted array of structures mys2c = @(s) squeeze(struct2cell(s))';   %'# helper function show results mys2c(s_str) mys2c(s_num) 

sorting field str gave:

>> mys2c(s_str) ans =      'cbawoj'    [ 0.10401]     'fqwiek'    [ 0.17567]     'fskvdc'    [ 0.46847]     'hezhbh'    [ 0.33585]     'kyeaxv'    [ 0.67539]     'ooumrm'    [ 0.20895]     'qbnqit'    [ 0.90515]     'wcwyjs'    [0.056705]     'wdyhlz'    [ 0.52189]     'ytdoze'    [ 0.91213] 

while sorting field num:

>> mys2c(s_num) ans =      'wcwyjs'    [0.056705]     'cbawoj'    [ 0.10401]     'fqwiek'    [ 0.17567]     'ooumrm'    [ 0.20895]     'hezhbh'    [ 0.33585]     'fskvdc'    [ 0.46847]     'wdyhlz'    [ 0.52189]     'kyeaxv'    [ 0.67539]     'qbnqit'    [ 0.90515]     'ytdoze'    [ 0.91213] 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -