Bar diagram as subplot in a for loop from a cell
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Patrick Petre
le 28 Nov 2020
Commenté : Patrick Petre
le 29 Nov 2020
So i have a for loop which calculates some data and after i plot everything i want a subplot as a bar diagramm. I fit a linear curve to my data and want the gradient of the linear regression as the bar value. for each dataset is should take out the first value. If i try it with a simple example as at the end with a and b(i) it works fine but my bar diagramm only shows the value of p{1,1}(1) with the color of c{2} ? I am so confused and thankful for evey help.
number = numel(data)
for i = 1:number
p{i} = polyfit(x_relevant{i}, y_relevant{i},1)
subplot(4,1,4)
b(i) = bar(p{1,i}(1),'FaceColor',c{i}) % each data set, take out first value
hold on
a = {[1 2], [3 4]}
b(i) = a{1,i}(1)
end
0 commentaires
Réponse acceptée
Adam Danz
le 28 Nov 2020
Modifié(e) : Adam Danz
le 28 Nov 2020
The goal is unclear but I think this is what your looking for,
number = numel(data);
for i = 1:number
p{i} = polyfit(x_relevant{i}, y_relevant{i},1);
end
coefs = cell2mat(p(:));
subplot(4,1,4)
b = bar(coefs(:,1));
bh.FaceColor = 'flat';
bh.CData = c; % where c is something like c=jet(numel(b.XData));
If not, please provide more details.
3 commentaires
Adam Danz
le 28 Nov 2020
I'd be happy to.
The main idea is to use the loop to gather the data and then organize the data into a matrix rather that using the loop to do the plotting. If you want a single bar plot with n-bars, you need a vector of n-values. Instead, your vector only contained 1 value on each iteration and you were merely adding another bar on top of the existing bars at x=1.
number = numel(data); % <--- minor, but add semicolon suppression
for i = 1:number
p{i} = polyfit(x_relevant{i}, y_relevant{i},1)
subplot(4,1,4) % <--- not necessary to call within the loop
b(i) = bar(p{1,i}(1),'FaceColor',c{i}) % <--- **All bars will be at x=1, on top of each other
hold on % <--- not necessary at all
a = {[1 2], [3 4]} % <--- ??? this is a mystery to me
b(i) = a{1,i}(1) % <--- ??? this is a mystery to me
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Line Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!