MATLAB Answers

how to plot four different variables sharing same axis but with different x and y axis limits

1 view (last 30 days)
SURESH KUMAR
SURESH KUMAR on 11 Dec 2020
Answered: Nitin Kapgate on 18 Dec 2020
i have saved four variables from a GUI, it consised of four signals with respect to time, now i have four variables having different data of signals with respect to their capture time. i want to plot these variables in single plot sharing same x and y axis but with thier own limits lets say
s1 was captures from 0.5 to 1 hours, and s2 from 5-5.5hours now i want to plot them all togeter but first s1 should start in pplot and secondaly s2, in that case the xa axis would be from 0to6 hours, how to do that?
variable are capured from figure 1 grapgh, using brush tool in matlab 2019 are saved s1 s2 s3 and s4, here in this code i have not mentioned about those variables, to run this code you can take my data text file that should be imported to workspace using clumn vector style when impoting. thanks
close all;
% j = 0;
% k = 1;
% m = length (heaterdata);
% Theater = zeros (7173.1);
% for j = 1: m
% str = heaterdata (j);
% word = strread (str, '% 11s');
% if mod (j, 2) == 0
% Theater (k) = str2num (word {2});
% k = k + 1;
% end
% end
% time_raw = data {:, 1};
time_raw = Number;
n = length (time_raw);
window = 64;
Ihigh = 0.0165;
Ilow = 0.005;
Kcement = 0.4;
PowerCh1 = PWMupCh0Ohm;
PowerCh2 = PWMupCh1Ohm;
PowerCh3 = PWMupCh2Ohm;
PowerCh4 = PWMupCh3Ohm;
PowerCh5 = PWMupCh4Ohm;
PowerCh6 = PWMupCh5Ohm;
Pt1000 = Rpt1kOhm;
flux = zeros (n, 1);
distance = 0.03;
time = zeros (n, 1);
PowMeas1 = zeros (n, 1);
PowMeas2 = zeros (n, 1);
PowMeas3 = zeros (n, 1);
PowMeas4 = zeros (n, 1);
PowMeas5 = zeros (n, 1);
PowMeas6 = zeros (n, 1);
PowMeas1n = zeros (n, 1);
PowMeas2n = zeros (n, 1);
PowMeas3n = zeros (n, 1);
PowMeas4n = zeros (n, 1);
PowMeas5n = zeros (n, 1);
PowMeas6n = zeros (n, 1);
Tamb = zeros (n, 1);
% PowMeas5 = zeros (n, 1);
% PowMeas6 = zeros (n, 1);
% Tempsector1 = zeros (n, 1);
% Tempsector2 = zeros (n, 1);
% Tempsector3 = zeros (n, 1);
% Tempsector4 = zeros (n, 1);
% Tempsector5 = zeros (n, 1);
% Tempsector6 = zeros (n, 1);
period = 31250;
roA = 176.5 / (1 + 0.003 * 21);
roB = 179.2578 / (1 + 0.003 * 21);
roC = 172.6640 / (1 + 0.003 * 21);
ro2A = 172.996 / (1 + 0.003 * 21);
ro2B = 189.098 / (1 + 0.003 * 21);
ro2C = 174.5429 / (1 + 0.003 * 21);
for i = 1: n
time (i) = ((time_raw (i) / 40000000) * 16777216) / 3600;
end
PowMeas1 = (PowerCh1 / period) * 100;
PowMeas2 = (PowerCh2 / period) * 100;
PowMeas3 = (PowerCh3 / period) * 100;
PowMeas4 = (PowerCh4 / period) * 100;
PowMeas5 = (PowerCh5 / period) * 100;
PowMeas6 = (PowerCh6 / period) * 100;
Tamb = (Pt1000-1000) /3.85;
Ttarget = (113.367 / 100-1) /0.00386;
avg = (PowMeas1 + PowMeas2 + PowMeas4 + PowMeas5) / 4;
% flux (i) = ((Theater (i) -Tamb (i)) * Kcement) / distance;
PowMeas1n = PowMeas1. / Avg;
PowMeas2n = PowMeas2. / Avg;
PowMeas3n = PowMeas3. / Avg;
PowMeas4n = PowMeas4. / Avg;
PowMeas5n = PowMeas5. / Avg;
PowMeas6n = PowMeas6. / Avg;
% PowMeas3 (i) = (PowerCh3 (i) / period) * 100;
% PowMeas5 (i) = (PowerCh5 (i) / period) * 100;
% PowMeas6 (i) = (PowerCh6 (i) / period) * 100;
% Tempsector1 (i) = ((ResCh1 (i) / roA-1) /0.003);
% Tempsector2 (i) = ((ResCh2 (i) / roB-1) /0.003);
% Tempsector3 (i) = ((ResCh3 (i) / roC-1) /0.003);
% Tempsector4 (i) = ((ResCh4 (i) / ro2A-1) /0.003);
% Tempsector5 (i) = ((ResCh5 (i) / ro2B-1) /0.003);
% Tempsector6 (i) = ((ResCh6 (i) / ro2C-1) /0.003);
convP1 = conv (PowMeas1, ones (1, window) / window, 'same');
convP2 = conv (PowMeas2, ones (1, window) / window, 'same');
convP3 = conv (PowMeas3, ones (1, window) / window, 'same');
convP4 = conv (PowMeas4, ones (1, window) / window, 'same');
convP5 = conv (PowMeas5, ones (1, window) / window, 'same');
convP6 = conv (PowMeas6, ones (1, window) / window, 'same');
convPn1 = conv (PowMeas1n, ones (1, window) / window, 'same');
convPn2 = conv (PowMeas2n, ones (1, window) / window, 'same');
convPn3 = conv (PowMeas3n, ones (1, window) / window, 'same');
convPn4 = conv (PowMeas4n, ones (1, window) / window, 'same');
convPn5 = conv (PowMeas5n, ones (1, window) / window, 'same');
convPn6 = conv (PowMeas6n, ones (1, window) / window, 'same');
convPt = conv (Tamb, ones (1, window) / window, 'same');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%
Imax = 16.5e-3;
Imin = 5e-3;
Rtarget = 113,367;
P1 = Imin ^ 2 * Rtarget * (100-convP1) / 100 + Imax ^ 2 * Rtarget * convP1 / 100;
P2 = Imin ^ 2 * Rtarget * (100-convP2) / 100 + Imax ^ 2 * Rtarget * convP2 / 100;
P4 = Imin ^ 2 * Rtarget * (100-convP4) / 100 + Imax ^ 2 * Rtarget * convP4 / 100;
P5 = Imin ^ 2 * Rtarget * (100-convP5) / 100 + Imax ^ 2 * Rtarget * convP5 / 100;
Off = (P1 + P2 + P4 + P5) / 4;
figure;
plot (time, (P1-Off) * 1000, 'r', 'linewidth',. 5)
hold on;
plot (time, (P2-Off) * 1000, 'b', 'linewidth',. 5)
hold on;
% plot (time_good, convP3_good. / O, 'g', 'linewidth',. 5)
hold on;
plot (time, (P4-Off) * 1000, 'm', 'linewidth',. 5)
hold on;
plot (time, (P5-Off) * 1000, 'c', 'linewidth',. 5)
hold on;
% plot (time_good, convP6_good. / O, 'y', 'linewidth',. 5)
xlabel ('time [h]');
ylabel ('Power-P avg [mW]');
legend ('Location', 'best');
legend ('Ch0 (up)', 'Ch1 (down)', 'Ch3 (up)', 'Ch4 (down)');
grid on;
% xlim ([0 6]);
% figure; plot (hf); grid on
figure;
plot (time, (Off) * 1000, 'k', 'linewidth',. 5)
xlabel ('time [h]');
ylabel ('P avg [mW]');
grid on;
% xlim ([0 6]);
ylim ([0 20]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% n20 = 4; n30_8 = n20; n81 = n30_8;
% while time (n20) <20
% n20 = n20 + 1;
% end
% while time (n30_8) <30.8
% n30_8 = n30_8 + 1;
% end
% while time (n81) <80.8
% n81 = n81 + 1;
% end
O = 1/4 * (convP1 + convP2 + convP4 + convP5);
P0 = convP1-O;
P1 = convP2-O;
P3 = convP4-O;
P4 = convP5-O;
hf_estimator = (P0 + P1). ^ 2+ (P3 + P4). ^ 2+ (P0-P1). ^ 2+ (P3-P4). ^ 2;
figure; plot (time, hf_estimator); grid on; ylim ([0,1000]);
figure; plot (time, O); grid on
Imax = 16.5e-3;
Imin = 5e-3;
Rtarget = 113,367;
P1 = Imin ^ 2 * Rtarget * (100-convP1) / 100 + Imax ^ 2 * Rtarget * convP1 / 100;
P2 = Imin ^ 2 * Rtarget * (100-convP2) / 100 + Imax ^ 2 * Rtarget * convP2 / 100;
P4 = Imin ^ 2 * Rtarget * (100-convP4) / 100 + Imax ^ 2 * Rtarget * convP4 / 100;
P5 = Imin ^ 2 * Rtarget * (100-convP5) / 100 + Imax ^ 2 * Rtarget * convP5 / 100;
Off = (P1 + P2 + P4 + P5) / 4;
figure; subplot (2,1,1);
plot (time, (P1-Off) * 1000, 'r', 'linewidth',. 5)
hold on;
plot (time, (P2-Off) * 1000, 'b', 'linewidth',. 5)
hold on;
% plot (time_good, convP3_good. / O, 'g', 'linewidth',. 5)
hold on;
plot (time, (P4-Off) * 1000, 'm', 'linewidth',. 5)
hold on;
plot (time, (P5-Off) * 1000, 'c', 'linewidth',. 5)
hold on;
% plot (time_good, convP6_good. / O, 'y', 'linewidth',. 5)
xlabel ('time [h]');
ylabel ('Power-P avg [mW]');
legend ('Location', 'best');
legend ('Ch0 (up)', 'Ch1 (down)', 'Ch3 (up)', 'Ch4 (down)');
grid on;
% xlim ([0 6]);
% figure; plot (hf); grid on
subplot (2,1,2);
plot (time, (Off) * 1000 * 4, 'k', 'linewidth',. 5)
xlabel ('time [h]');
ylabel ('Total power of sphere [mW]');
grid on;
% xlim ([0 6]);
% ylim ([0 20]);
px = (P1-Off) * 1000;
py = (P2-Off) * 1000;
pz = (P4-Off) * 1000;
pa = (P5-Off) * 1000;
figure;
% McIdx1 = (time> = 9.3) & (time <= 9.8); second sample
% McIdx2 = (time> = 11) & (time <= 11.5); 3rd sample
% McIdx3 = (time> = 2) & (time <= 2.5); second sample
% Select Elements (Logical Vector)
% time = rand (n, 1);
% an example
McIdx = (time> = 8.5) & (time <= 10);
plot (time (McIdx), px (McIdx), 'r', 'linewidth',. 5)
hold on
plot (time (McIdx), py (McIdx), 'b', 'linewidth',. 5)
% Plot Range
hold on
plot (time (McIdx), pz (McIdx), 'm', 'linewidth',. 5)
% Plot Range
hold on
plot (time (McIdx), pa (McIdx), 'c', 'linewidth',. 5)
% Plot Range
hold on
xlabel ('time [h]');
ylabel ('Power-P avg [mW]');
legend ('Location', 'best');
legend ('Ch0 (up)', 'Ch1 (down)', 'Ch3 (up)', 'Ch4 (down)');
grid on;

Answers (1)

Nitin Kapgate
Nitin Kapgate on 18 Dec 2020
You can refer the following code snippet which sets and uses multi-row tick labels to resolve your problem.
close all;
% Data mimicking 4 different time intervals
x1 = 1:1:50;
x2 = 51:1:100;
x3 = 101:1:150;
x4 = 151:1:200;
y1 = 21:1:70;
y2 = 71:1:120;
y3 = 121:1:170;
y4 = 171:1:220;
% Figure without multiple tickLabels
f1= figure;
plot(x1,y1);
hold on
plot(x2,y2);
hold on
plot(x3,y3);
hold on
plot(x4,y4);
hold off
% Figure with multiple tickLabels
f2 = figure;
plot(x1,y1);
hold on
plot(x1,y2);
hold on
plot(x1,y3);
hold on
plot(x1,y4);
hold off
% Define each row of labels.
% We need 4 labels
% Labels can be modified as per the requirement
row1 = 1:7;
row2 = 8:14;
row3 = 15:21;
row4 = 22:28;
% Combine the rows of labels into a cell array; convert non-strings to strings/character vectors.
% labelArray is an nxm cell array with n-rows of m-tick-lables.
labelArray = [compose('%d',row1);compose('%d',row2); compose('%d',row3);compose('%d',row4)];
% To use right or center justification,
% labelArray = strjust(pad(labelArray),'center'); % 'left'(default)|'right'|'center
% Combine the rows of labels into individual tick labels
% Change the compose() format according to your label classes.
% Place the \\newline command between rows of labels.
% This plot has 4 rows of labels so there are 3 \\newline commands.
tickLabels = strtrim(sprintf('%s\\newline%s\\newline%s\\newline%s\n', labelArray{:}));
% Assign ticks and labels
ax = gca();
ax.XTick = 1:7;
ax.XLim = [1,8];
ax.XTickLabel = tickLabels;

Community Treasure Hunt

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

Start Hunting!