setting YTickLabel matlab - matlab

what is wrong that i can not figure out in my YTickLabel:
h2=bar(myData);
ylabels=['1';'1.5';'2';'2.5';'3'];
set(gca,'XTickLabel',applicationNames),'XTick',applicationNames),'YTickLabel',ylabel));
p.s: I have tried this as well with no success:
ax=gca
ax.YTickLabel=['1';'1.5';'2';'2.5';'3'];
I am getting this error:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.

You are trying to create a character array. In this case, you are trying to create a 2D matrix where the number of columns should have the same number of characters and the number of rows denotes how many labels you have. For your strings, the maximum number of characters per column is three (number / dot / number). Because you have characters that are only of length 1 (i.e. just a number), you are getting an inconsistent concatenation error because it's expecting all characters to be of length 3 in the array.
What you actually need to use is a cell array to accommodate for the inconstant size of each y tick label. Therefore:
ax.YTickLabel={'1';'1.5';'2';'2.5';'3'};
Alternatively, because your labels are numbers, you can simply use a numeric array instead:
ax.YTickLabel = [1;1.5;2;2.5;3];
A cell array of characters is used if you want to label the x and/or y axis to be something other than just numbers. It's possible to label the y axis using text, such as:
ax.YTickLabel = {'John'; 'Paul'; 'George'; 'Ringo'; 'The Beatles'};

Related

MATLAB Heatmap dendrogram not showing column names when there are many names

I have a list of a proteins and values for each protein based on three different experimental conditions (alpha, beta and gamma). The array containing the values is called 'heatmap_data'. The name of the proteins is in the array called: 'text'
I generated a heatmap:
rows = ['ALPHA' ;'BETA '; 'GAMMA']
rowscell = cellstr(rows)
dm=DataMatrix(heatmap_data,rowscell,text);
cg = clustergram(dm,'Standardize','none');
cgAxes =plot(cg);
set(cgAxes, 'Clim', [-1,1])
When the list of proteins is short, I got the expected heatmap, showing labels for x axis
However, when the list extens to few hundreds, the names disappear.
I can understand that the labels might not fit in the short space, but if they were written I could reduce font size, or expand the dendrogram, etc
My question: is there a way to force MATLAB to show the column names even if they overlap, or a function I can save the names in the same order the dendrogram ordered so I can identify which proteins are in each cluster?
Thanks
Ok, I found this:
https://www.mathworks.com/help/bioinfo/ref/clustergram.html
RowLabelsValue Vector of numbers or cell array of character vectors
to label the rows in the dendrogram and heat map. Default is a vector
of values 1 through M, where M is the number of rows in Data. Note:
If the number of row labels is 200 or more, the labels do not appear
in the clustergram plot unless you zoom in on the plot.
Now, If I zoom I can see the names.

How to fix the decimal place of matrix elements in matlab?

I have a matrix of order 3 x 3, and all elements of matrix are up to 6 decimal place. I want to display this elements of matrix only up to 5 decimal place. I used format short and format long, but it gives either 4 or 15 decimal places.
Is there a command that gives up to any particular decimal places?
I have idea for a single number but could not solve for all entries of a matrix.
The builtin format options cannot handle this. You'll instead want to use fprintf or num2str (with a format specifier) to force the appearance of the number
data = rand(3) * 100;
num2str(data,'%12.5f')
% 20.42155 3.95486 91.50871
% 9.28906 87.24924 72.61826
% 47.43655 95.70325 94.41092
If you want to make this the default display at the command line you could overload the builtin display method for double but I would not recommend that.
Alternately, you can use vpa to specify the number of significant digits to display (note that the second input is the number of significant digits and not the number of numbers after the radix point).
vpa(data, 5)

Matlab xlsread: force empty cells to be read as NaN

I need to import data from a square region (10 by 10 cells) on an Excel sheet into Matlab.
All data in the region are numerical, but some outer rows and columns of the region are empty.
In Matlab I still want to have a 10 by 10 matrix of doubles with NaNs in places where there are empty cells in Excel (also in outer rows and columns).
If I use xlsread then empty outer rows and columns are automatically truncated.
Needless to say that all should be done automatically without the knowledge how many empty outer rows and columns are there.
How can I do this?
Let's say your 10 by 10 spreadsheet's first row and column and last row and column are empty (like this). Using:
[num,txt,raw] = xlsread('myfile.xlsx',1,'A1:J10'); % Read input.
will return:
num 8x8 double
txt 0x0 cell
raw 10x10 cell
In num, non-scalar leading rows and columns are automatically truncated, while in txt any numerical values are omitted. However, raw contains all information, so it can be used to extract the numerical values:
raw(cellfun(#ischar,raw)) = {NaN}; % Set non-scalar values to missing.
A = cell2mat(raw); % Convert to matrix.

check how many times a column of an array is equal to a vector in matlab

I have an image converted with the use of im2cal in an array of 9 rows and 3867208 columns and i want to count how many times a vector containing a mask is equal to each column of the image without using for loops in matlab.Do you have any ideas?

Identifying uniques in a cell array

I have a 45x2 cell in MATLAB, with the first column an arbitrarily sized matrix of doubles.
Some of these matrices are repeated, whilst others aren't. I'm attempting to strip out only the unique matrices (but recording the number of repeates), and keep the second column as is.
I've tried a number of things (tabulate, hist et al) but they all fail because of the cell structure (I think). How would one go about doing this, short of looping through each of them individually?
If you convert your matrices to strings, you can run unique on them:
%# create a sample cell array
mc = {magic(3);magic(4);magic(4);magic(5);magic(3);magic(4)}
%# convert to strings
mcs = cellfun(#(x)(mat2str(x)),mc,'uniformoutput',false);
%# run unique
[uniqueCells,idxOfUnique,idxYouWant] = unique(mcs);