Matlab boxplot for multiple fields - matlab

I have this matlab file which has a field called "data". In "data" I have lots of fields for different bonds (x5Q12... etc).
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.

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.

B=gilts_withoutdates.data;
b=fieldnames(B);
for a=1:numel(b)
boxplot(B.(b{a})); fig;
end
To plot a boxplot for each of the 5 columns of data for each field you could do this:
pos=1;
for i = 1:numel(b)
for ii=1:5
subplot(numel(b),5,pos);boxplot(B.(b{i})(:,ii));pos=pos+1;
end
end

Related

Looping through structure in matlab to sort

I have a structure with 7 fields. In each cell of the 7th field there is a date. I need a nested loop to sort through those dates and if any of those dates do not exist in the 2nd field of the same structure I would like to take that information (from all 7 fields) and create a new structure containing only the iterations that has dates in the 2nd field but not the 7th. The code that I have so far is:
for i=1:12 %number of dates in the 7th field
for j=1:length(files_cdf) %number of dates in the 2nd field
y(i,j)=isequal(files_cdf(j).date,files_cdf(i).deletables);
if isequal(y(i,j),0)
cdf(j)=files_cdf(j);
end
end
end
But it copies my entire original structure into the new structure without removing the unwanted information. Any help would be greatly appreciated!
I think the loops can be eliminated through some vectorization via bsxfun assuming the elements are scalars of type datetime:
% Convert to double
second = datenum([files_cdf(:).date].');
seventh = datenum([files_cdf(:).deletables]);
%
% Make logical mask
mask = all(bsxfun(#ne,second,seventh),2)
%
% Mask struct array
cdf = files_cdf(mask);

MATLAB loading data from multiple .mat files

My data is x,y co-ordinates in multiple files
a=dir('*.mat')
b={a(:).name}
to load the filenames in a cell array
How do I use a loop to sequentially load one column of data from each file into consecutive rows of a new/separate array......?
I've been doing it individually using e.g.
Load(example1.mat)
A(:,1)=AB(:,1)
Load(example2.mat)
A(:,2)=AB(:,1)
Load(example3.mat)
A(:,3)=AB(:,1)
Obviously very primitive and time consuming!!
My Matlab skills are weak so any advice gratefully received
Cheers
Many thanks again, I'm still figuring out how to read the code but I used it like this;
a=dir('*.mat');
b={a(:).name};
test1=zeros(numel(b),1765);
for k=1:numel(b) S=load(b{k});
I then used the following code to create a PCA cluster plot
test1(k,:)=S.AB(:,2); end [wcoeff,score,latent,tsquared,explained] = pca(test1,... 'VariableWeights','variance');
c3 = wcoeff(:,1:3) coefforth = inv(diag(std(test1)))*wcoeff; I = c3'*c3 cscores = zscore(test1)*coefforth;
figure() plot(score(:,1),score(:,2),'+') xlabel('1st Principal Component') ylabel('2nd Principal Component') –
I was using 'gname' to label the points on the cluster plot but found that the point were simply labelled from 1 to the number of rows in the array.....I was going to ask you about this but I found out simply through trial and error if I used 'gname(b)' this labels the points with the .names listed in b.....
However the clusterplot starts to look very busy/messy once I have labelled quite a few points so now I am wondering is is possible to extract the filenames into a list by dragging round or selecting a few points, I think it is possible as I have read a few related topics.....but any tips/advice around gname or labelled/extracting labels from clusterplots would be greatly appreciated. Apologies again for my formatting I'm still getting used to this website!!!
Here is a way to do it. Hopefully I got what you wanted correctly :)
The code is commented but please ask any questions if something is unclear.
a=dir('*.mat');
b={a(:).name};
%// Initialize the output array. Here SomeNumber depends on the size of your data in AB.
A = zeros(numel(b),SomeNumber);
%// Loop through each 'example.mat' file
for k = 1:numel(b)
%// ===========
%// Here you could do either of the following:
1)
%// Create a name to load with sprintf. It does not require a or b.
NameToLoad = sprintf('example%i.mat',k);
%// Load the data
S = load(NameToLoad);
2)
%// Load directly from b:
S = load(b{k});
%// ===========
%// Now S is a structure containing every variable from the exampleX.mat file.
%// You can access the data using dot notation.
%// Store the data into rows of A
A(k,:) = S.AB(:,1);
end
Hope that is what you meant!

MATLAB Box Plot for large groups of Measurements

I've got basically three large groups of measurements and im trying to generate a BoxPlot with 4 Boxes. One box for each group and the last one for all groups joined.
I've tried with this code
A = rand(1417725,1)
B = rand(2236508,1)
C = rand(3100641,1)
D = [A;B;C]
X= [A;B;C;D]
group = [repmat({'a'},1417725,1); repmat({'b'},2236508,1); repmat({'c'},3100641,1); repmat({'d'},6754874,1)];
boxplot(X,group)
but at the end i get " Out of memory" and i can't get the plot.
do you have any idea to solve this problem??
Thank you!
Instead of creating a huge (in terms of memory) cell array of strings, create a much smaller array of for instance int8 integers:
group = [ones(size(A),'int8');2*ones(size(B),'int8');3*ones(size(C),'int8');4*ones(size(D),'int8')];
Then, after plotting, change the labels in the plot to the desired names:
set(gca, 'XTick', 1:4, 'XTickLabel', {'a','b','c','d'});
Maybe you have enough memory to do it this way..

Indexing data in matlab

I have imported a lot of data from an excel spreadsheet so that I have a 1x27 matrix.
I have imported data from excel using this
filename = 'for_matlab.xlsx';
sheet = 27;
xlRange = 'A1:G6';
all_data = {};
for i=1:sheet,
all_data{i} = xlsread(filename, i, xlRange);
end
However each element of this all_data matrix (which is 1x27) contains my data but I'm having trouble accessing individual elements.
i.e.
all_data{1}
Will give me the entire matrix but I need to perform multiplications on individual elements of this data
also
all_data(1)
just gives '5x6 double', i.e. the matrix dimensions.
Does anybody know how I can divide all elements of each row by the third element in each row and do this for all of my 'sub-matrices' (for want of a better word)
Assuming that all_data is a cell array and that each cell contains a matrix (with at least three columns):
result = cellfun(#(x) bsxfun(#rdivide, x, x(:,3)), all_data, 'uniformoutput', 0);
You are mixing terminology in matlab. what you have is 1x27 CELLS each of them containing a matrix.
If you access all_data{1} it will give you the whole matrix stored in the first cell.
If you want to access the elemets of that matrix then you need to do: all_data{1}(2,4). This example access the 2,4 element of the matrix in the first cell.
Definitely Luis Mendo has solved you problem, but be aware of the differences of Cells and matrixes in Matlab!
Okay I have found the answer now.
Basically you have to use both types of brackets because the data types are different
i.e. all_data{1}(1:4) or something like that anyway.
Cheers

Setting Column and Row Names from Cell in uitable - MATLAB

I am using GUIDE to build my first GUI interface in MATLAB.
I have several matrices I want to display using the uitable. For now let's focus on one matrix, say myMatrix [10x5]
Now I have two cells of strings, columnNames (1x5), and another, rowNames (10x1). I would like to set these cells to the row and column names of the table, but I cant yet figure out how to do this.
The MATLAB help page says you can use a cell of strings to do this, however in the property inspector, and under ColumnName, the only non-numeric option is to enter the names manually.
Any help would be appreciated (or suggestions to go about this in a different way).
In order to have custom Row/Column Names you have to pass a cell of strings (using {<names>}) into the ColumnName and RowName properties of the uitable. Here is an example directly from MatLab's uitable documentation:
f = figure('Position',[200 200 400 150]);
dat = rand(3);
cnames = {'X-Data','Y-Data','Z-Data'}; % These are your column names
rnames = {'First','Second','Third'}; % These are your row names
t = uitable('Parent',f,'Data',dat,'ColumnName',cnames,...
'RowName',rnames,'Position',[20 20 360 100]);
When parsing you're file, be sure to create the lists as a cell of strings.