Search code examples
matlabmultiple-databasesboxplot

Matlab boxplot for multiple fields


I have this matlab file which has a field called "data". In "data" I have lots of fields for different bonds (x5Q12... etc). enter image description here

I am trying to produce ONE box plot that contains ONE column from each of the fields (i.e. a box diagram with 36 boxes in it). I tried this code (e.g. to plot a box for column 2 in all of the bonds) but it does't work for me:

boxplot(gilts_withoutdates.data.:(:,2));figure(gcf);

I know my understanding of calling different levels in a structure is a problem here. Any suggestions, please? Many thanks.


Solution

  • You can use STRUCTFUN to extract the data from a particular column of all fields of a structure.

    col2plot = 2; %# this is the column you want to plot
    
    %# return, for each field in the structure, the specified
    %# column in a cell array
    data2plot = structfun(@(x){x(:,col2plot)},gilts_withoutdates.data);
    
    %# convert the cell array into a vector plus group indices
    groupIdx = arrayfun(@(x)x*ones(size(data2plot{x})),1:length(data2plot),'uni',0);
    groupIdx = cat(1,groupIdx{:});
    data2plot = cat(1,data2plot{:});
    
    %# create a compact boxplot
    boxplot(data2plot,groupIdx,'plotStyle','compact','labels',labels)
    

    If you're interested in the distribution of the data, I can recommend my function distributionPlot.