How do I stack an array variable and plot three of these stacked array variables on a barh? - matlab

% S
a1 = [2015/07/23 2015/11/25 2016/01/20];
b1 = [2011/06/22 2014/10/14 2015/03/01];
c1 = [2012/04/16 2013/06/23 2015/04/08];
d1 = [2013/09/15 2014/01/19 2016/09/13];
e1 = [2015/04/01 2016/04/04 2018/08/04];
% H
a2 = [2012/07/23 2015/06/25 2016/05/20];
b2 = [2009/06/22 2014/09/14 2015/11/01];
c2 = [2006/04/16 2013/12/23 2015/06/08];
d2 = [2008/09/15 2014/05/19 2016/02/13];
e2 = [2011/04/01 2016/05/04 2018/03/04];
% HS
a3 = [2009/07/23 2010/06/25 2018/02/20];
b3 = [2011/06/22 2014/07/14 2016/09/01];
c3 = [2013/04/16 2016/09/23 2019/05/08];
d3 = [2013/09/15 2018/05/19 2019/06/13];
e3 = [2014/04/01 2019/01/04 2019/12/04];
% T
t = [1 2 3 4 5];
dates = [a1 a2 a3; b1 b2 b3; c1 c2 c3; d1 d2 d3; e1 e2 e3];
% Plotted
figure
barh(t, dates, 'hist')
title('Script')
xlabel('Time')
ylabel('Tail')
legend({'S','H','HS'})
legend('Location', 'southoutside')
legend('Orientation','horizontal')
If you plot this, you will notice that there are 9 bar graphs associated with each 't'. There should be only three as stated in the 'dates' variable per t. How do I stack 'a1,a2,a3, then b1,b2,b3 ...... and e1,e2,e3 each individually' to accomplish this feat?
My script result:
What I want the output to look like
Note: 1. The y axis contains the 5 different elements 't'
2. The x axis should contain the date elements 'dates'
3. When you plot these values, there are 9 bars. There should be three per
't'.
4. On the x axis, I would like to have the dates represented.
5. I would like to eventually be able to create a user prompted system that
allows people to enter in dates for a corresponding array, and have that
date be stacked onto the chart.
The following bit of code does a bit of what I ask for but with the bars stacked vertically not horizontally, and also takes in different user inputs.
https://www.mathworks.com/matlabcentral/fileexchange/32884-plot-groups-of-stacked-bars

You are very close to a solution. Download that file from MATLAB file exchange, open it, replace bar with barh, you got your own horizontally stacked bar plot.
Your input does not fit the expected format, the function expects a 3d matrix. A minor change to your code:
dates = [cat(3,a1,a2,a3);cat(3,b1,b2,b3); cat(3,c1,c2,c3); cat(3,d1,d2,d3); cat(3,e1,e2,e3)];
plotBarHStackGroups(dates,t)

Related

FInd the intersect between timestamps in Matlab

I want to find the intersect between 2 time vectors t1 and t5 which have some gaps marked by the stars as in the figure. Because intersect function in matlab just find exactly value so I have to use ismembertol. My result is the middle line, which is missing the gap information in the t5 vector. How can I achieve this? This is my code:
`
tol = 1e-08; Fs = 50;
[a,b] = ismembertol(t1,t5,tol);
tcom15 = t1(a);
t1gap = t1(find(round(diff(t1)* 86400*Fs)>1));
t5gap = t5(find(round(diff(t5)* 86400*Fs)>1));
tcom15gap = tcom15(find(round(diff(tcom15)* 86400*Fs)>1));
figure; plot(t1,2*ones(length(t1),1)); hold on
plot(t5,3*ones(length(t5),1));ylim([1 4])
plot(t1gap,2*ones(length(t1gap),1),':*','MarkerSize',5)
plot(t5gap,3*ones(length(t5gap),1),':*','MarkerSize',10)
plot(tcom15,2.5*ones(length(tcom15),1))
plot(tcom15gap,2.5*ones(length(tcom15gap),1),':*','MarkerSize',10)

How can I define dynamically named matrices?

If you define a range from A1 to A10 in Excel using VBA you cam use Range("A1:A10"). On the other hand side it's possible to write down Range("A1:A"&10). How can I use the second way in MATLAB, please?
I have some matrices M1, M2, M3, ..., and I wish to define them by using iterator FOR that helps me stop writing the matrix names completely.
Notwithstanding the comment that this approach is frowned upon, below is a way to do it programmatically:
% define cell with matrix names
number_of_matrices_I_want = 5;
my_matrix_names = repmat({'NA'}, 1, number_of_matrices_I_want);
for ii = 1:length(my_matrix_names)
my_matrix_names(ii) = {strcat('M',num2str(ii))};
end
% example for how to populate a matrix from "my_matrix_names"
x = rand(5,1); % fake numbers
y = rand(5,1);
eval([my_matrix_names{1} '= [x y]']);

Curve fitting with self-defined equation

I am trying to fit the experimental data with a self-defined equation with MATLAB. The parameters I am interested are y0, tau, D and C. But I keep getting an error saying matrix dimensions must agree with the the following codes:
t = [0.00468
0.01277
0.05987
0.10316
0.18595
0.29252
0.39529
0.52136
0.68313
0.88818
1.08182
1.28688
1.45625
1.57471
1.73267
1.84685]
y = [9.02766
7.53879
5.3679
4.28093
3.09349
2.11005
1.63202
1.10224
0.77341
0.54506
0.42022
0.24363
0.21623
0.11575
0.11575
0.06704]
a1 = 10.86;
a2 = 15.5;
b1 = 8.74;
E = 1e15;
F = #(x,xdata)y0.*exp(-xdata./tau-(4.*pi./3).*E.*(1.7725).*(C.*xdata).^(1/2).*((1.+a1.*(D.*C.^(-1/3).*xdata.^(2/3)...
)+a2.*(D.*C.^(-1/3).*xdata.^(2/3)).^2)./(1.+b1.*(D.*C.^(-1/3).*xdata.^(2/3))).^(3/4)));
X0 = [1 1e-3 1e-13 1e-30];
[x,resnorm,~,exitflag,output] = lsqcurvefit(F,X0,t,y)
The equation is described in the inserted graph and an example of using the functiong to fit the data is shown in another graph. Thank you for your help.

error with an assignment A(I) = B, the number of elements in B and I must be the same

i have data set saved as .mat and i am trying to solve for a system of non-linear equations for unknown variables Ga and Ta. I'm using fsolve to solve it and the part of the relevant code is:
function F = msabase(x)
load ('matlab.mat');
Ta = x(1);
Ga = x(2);
util_a = exp(lamda.*(alpha_a - cost - w.*log(Ga)));
util_t = exp(lamda.*( - 2.5 - w.*log(2*0.80)));
F(1) = Ga - c0.*(1.+c1.*(Ta./cap).^c2).*d;
F(2) = Ta - sum.*(util_a/(util_a+util_t));
in each rows of the data set the values for all the other variables i.e lamda,alpha_a,cost, etc. are given. in line 7 of the code given, i'm getting the error "In an assignment A(I) = B, the number of elements in B and
I must be the same"
i'm not been able to understand why because it should be an element by element operation.
You are getting that error because you are trying to assign a vector / matrix of elements to a single slot in F. There is a dimension mismatch because you are trying to map more than one value into a single space in F, and that's ultimately why you are getting the error.
One suggestion I have is to either use cell arrays or create a 2D matrix that stores your values. If you prefer the cell array approach, each cell stores the desired calculation like so:
F = cell(2,1);
F{1} = Ga - c0.*(1.+c1.*(Ta./cap).^c2).*d;
F{2} = Ta - sum.*(util_a/(util_a+util_t));
Then to access the right slot, do either F{1} or F{2}. If you want the 2D matrix approach, you can concatenate both calculations into a single matrix by doing this:
F = [Ga - c0.*(1.+c1.*(Ta./cap).^c2).*d; Ta - sum.*(util_a/(util_a+util_t))];
This is assuming that each result is a single row vector, and so this will produce a 2D matrix where each row is the desired result. I'm not sure what size each computation is, and so to make things consistent, I'll make sure that both lines of code are row vectors:
F1 = Ga - c0.*(1.+c1.*(Ta./cap).^c2).*d;
F2 = Ta - sum.*(util_a/(util_a+util_t));
F = [F1(:).'; F2(:).'];
Try preallocating F before assignment. If you know that F is a 2-by-1 vector, insert F = zeros(2,1) somewhere before line 7. If you know nothing about the dimensions of F, initialise it as an empty matrix and append to it:
F = []
F = [F; (Ga - c0.*(1.+c1.*(Ta./cap).^c2).*d)];
F = [F; (Ta - sum.*(util_a/(util_a+util_t)))];
Beware that MATLAB is not particularly efficient at appending vectors/matrices, so preallocate if possible.

How to multiply the subsets from table?

I have two figures of table.
One table is call H and another one call C. Both table is 4 by 3 table.
So if the user insert a value in two edit box. For example:
A = *value*
B = *value*
Then the user insert the data in H table. The user only use 2 rows. Let say this is the data:
ALPHA BETA GAMMA
H1
H2
H3
H4
So the user want to get the subset of H1 and multiply with A and subsets of H2 multiply with B. This is how it will be:
C1 = (ALPHA VALUE)*A (BETA VALUE)*A (GAMMA VALUE)*A
C2 = (ALPHA VALUE)*B (BETA VALUE)*B (GAMMA VALUE)*B
Then the user wants to display the answer on C table where it will become like this:
ALPHA BETA GAMMA
C1 NEW VALUE NEW VALUE NEW VALUE
C2 NEW VALUE NEW VALUE NEW VALUE
C3
C4
How can i make the coding of this problem?
I have already try this coding but it seems i failed. CAN ANYONE HELP ME PLEASE!!
H = cell2mat(get(handles.Mytable3,'Data'));
cost1 = str2num(get(handles.input2_editText,'String'));
cost2 = str2num(get(handles.input3_editText,'String'));
H1 = H(1,:)*cost1;
H2 = H(2,:)*cost2;
H = mat2cell([H1 H2]);
cost = get(H,'Data');
set(handles.Mytable2,'Data',cost)
Try:
H = num2cell([H1 H2]);
set(handles.Mytable2,'Data',H)