How to add right ylabel with complex values (attached data and code)

1 view (last 30 days)
I have a matrix attached called "PF". I read the matrix and plot it using pcolor. This matrix has x-label named "x_state" and left y-axis named "DR"; both attached. I want to add ylabel to the righ axis giving the complex array called "EV" attached.
Any idea?
clear all; clc;
PF = xlsread('PF.xlsx'); % Main data to be plotted
DR = xlsread('DR.xlsx'); % used to label left y-axis
[~, TEXT, ~] = xlsread('States.xlsx'); x_state=TEXT; % used to label x-axis
[~, TEXT, ~] = xlsread('EV.xlsx'); EV=TEXT; % I want to add these complex values to the right y-axis
ylabel('DR (%)')
axis([65 96 65 96])

Answers (3)

Alex Mcaulley
Alex Mcaulley on 6 Mar 2019
Ismaeel on 6 Mar 2019
Thanks Alex for your efforts. Yes, but when I zoom in or out, the right y-axis labels do not change accordingly; they are consant unlike the left y-axis labels. Is there a way to solve this? Thanks once again.

Sign in to comment.

Star Strider
Star Strider on 6 Mar 2019
Try this:
x1 = sort(rand(1,10)); % Create Data
y1 = rand(1,10); % Create Data
x2 = sort(rand(1, 15)); % Create Data
y2 = rand(1, 15)+0.5; % Create Data
ax = gca;
yyaxis left
plot(x1, y1)
yyaxis right
plot(x2, y2)
yt2 = ax.YAxis(2).TickValues; % Get Current Yick Values
ytv = sort(exp(1j*rand(1,7))); % Define Complex YAxis(2) Tick Values
yt2new = linspace(min(yt2), max(yt2), numel(ytv)); % Create Tick Positions Corresponding To ‘ytv’
y2lbls = sprintfc('%.3f%+.3fj', ytv); % Create Complex YAxis(2) Tick Labels Cell Array: ‘sprintfc’
y2lbls = compose('%.3f%+.3fj', ytv); % Create Complex YAxis(2) Tick Labels Cell Array: ‘compose’
ax.YAxis(2).TickValues = yt2new; % Set New Tick Values
ax.YAxis(2).TickLabels = y2lbls; % Set New Tick Labels
You already have a vector for ‘ytv’, so substitute it for my vector. (Keeping the names the same with a different vector will simplify your using my code.)
Experiment to get the result you want.

Sign in to comment.

Ismaeel on 6 Mar 2019
Plotting is a basic problem in many fileds. I wonder how Matlab after years of feedbacks did not solve such a problem. The prolem is simple, one wants to plot data with additional right y-axis given by complex values. There should be no hard coding to plot such data.

Community Treasure Hunt

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

Start Hunting!

Translated by