How can I call RGB triplets within my for loops from a predefined structure?

17 views (last 30 days)
Hello!
I need some help with my code, since I am new to Matlab.
I attach part of it to help context-wise; however the purpose of the code is not important here. What I am trying to do to is to use pre-selected RGB triplets (see line 4) instead of using Matlab's default colours. For each figure (I will have a total of 31, one for each day of measurements) that my code will generate I will end up with five subplots, where each of them has to be in a different colour. Since I want to use RGB triplets, I have to call c (...) or c {...} within lines 30,32, 34 and 36 in such a way that when t == 1, I get the colour corresponding to [0, 0.4470, 0.7410], for t == 2 -> [0.8500, 0.3250, 0.0980] and so on.
I tried extracting subarrays, using cell arays etc., but I just don't succeed at getting the triplets enclosed in square brackets, the format that Matlab wants for creating the colours.
nd = 31;
np = 37;
c = ['r'; 'b'; 'm'; 'g'];
%r = [[0, 0.4470, 0.7410]; [0.8500, 0.3250, 0.0980]; [0.4660, 0.6740, 0.1880]; [0.6350, 0.0780, 0.1840]];
MM = [];
std_all = [];
%nd - day number
%nh - hour number
%np - no. of p levels
for d = 1 : nd
figure('units','normalized','outerposition',[0 0 1 1])
nh = 4*d;
j = 1;
for t = (nh-3) : nh
m_u1_1 = [];
s_u1_1 = [];
daily_std = [];
for i = 1 : np
temp = u_wind(:,:,i,t);
mean_u1 = mean(temp(:));
std_u1 = std(temp(:));
m_u1_1 = [m_u1_1, mean_u1];
s_u1_1 = [s_u1_1, std_u1];
std_day = std(u_wind(:,:,i,(nh-3):nh), 0, 'all');
daily_std = [daily_std, std_day];
end
hourly_std_subs = m_u1_1 - s_u1_1;
hourly_std_add = m_u1_1 + s_u1_1;
subplot(1,5,t)
plot(m_u1_1, plevel, [c(t,:) '-'], 'LineWidth', 2);
hold on
plot(hourly_std_subs, plevel, [c(t,:) '--'], 'LineWidth', 1);
hold on
plot(hourly_std_add, plevel, [c(t,:) '--'], 'LineWidth', 1);
hold on
patch([hourly_std_subs fliplr(hourly_std_add)], [plevel' fliplr(plevel')], c(t,:), 'EdgeColor', c(t,:), 'FaceAlpha', .3)

Accepted Answer

Adam Danz
Adam Danz on 23 Jun 2019
Edited: Adam Danz on 24 Jun 2019
%This line is fine
r = [[0, 0.4470, 0.7410]; [0.8500, 0.3250, 0.0980]; [0.4660, 0.6740, 0.1880]; [0.6350, 0.0780, 0.1840]];
% Set the "color" property for line objects
plot(m_u1_1, plevel, '-', 'Color', r(t,:), 'LineWidth', 2);
% This line below looks OK
patch([hourly_std_subs fliplr(hourly_std_add)], [plevel' fliplr(plevel')], r(t,:), 'EdgeColor', r(t,:), 'FaceAlpha', .3)

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!