How can I get Greek letters to show up in my legend, if my legend is automated through a loop?
66 views (last 30 days)
Show older comments
My code is here (with random matrices to produce variables, for simplification). My issue is with the legend function.I am currently getting my legend from a cell. This is important because I am using this script for data processing several files at once, and with the number of those files changing from run to run. However, I want the Greek character 'Phi' to show up in the legend, though MATLAB is not interpreting it as 'tex' and I am just getting the characters '/Phi' in the plot legend. I assume it is something to do with the elements in legend_cell being of type 'char' but I haven't figured out what to do about that. Any help is much appreciated.
My code is as follows (this should be able to reproduce the problem if copied and pasted):
clc;
clear all;
%Values
x1=rand(250,12);
x1(:,1)=(0.5);
y1=rand(300,12);
y1(:,1)=(0.5);
z1=rand(150,12);
z1(:,1)=(0.5);
x2=rand(255,12);
x2(:,1)=(0.75);
y2=rand(350,12);
y2(:,1)=(0.75);
z2=rand(450,12);
z2(:,1)=(0.75);
x3=rand(155,12);
x3(:,1)=(1.25);
y3=rand(250,12);
y3(:,1)=(1.25);
z3=rand(300,12);
z3(:,1)=(1.25);
numfiles = 9;
vals = cell(1,numfiles);
vals_length = zeros(1,numfiles);
vals{1}=x1;
vals{2}=y1;
vals{3}=z1;
vals{4}=x2;
vals{5}=y2;
vals{6}=z2;
vals{7}=x3;
vals{8}=y3;
vals{9}=z3;
%get max dimensions
max_dim=0;
for i = 1:numfiles
vals_length(i)=length(vals{i}(:,1));
if vals_length(i)>max_dim
max_dim=length(vals{i}(:,1));
end
end
zero_rows_reqd=zeros(1,numfiles);
for i = 1:numfiles
if vals_length(i)<max_dim
zero_rows_reqd(i) = max_dim-vals_length(i);
vals{i}=[vals{i}; zeros(zero_rows_reqd(i),12)];
end
end
%Initialize Variables
spark = zeros(1,numfiles);
phi_nominal = zeros(1,numfiles);
for i = 1:numfiles;
%Get variables
a(:,i) = vals{i}(1:max_dim,1);
b(:,i) = vals{i}(1:max_dim,2); %RPM
c(:,i) = vals{i}(1:max_dim,3); %deg_Celsius
d(:,i) = vals{i}(1:max_dim,4); %deg_Celsius
e(:,i) = vals{i}(1:max_dim,5); %deg_Celsius
f(:,i) = vals{i}(1:max_dim,6);
g(:,i) = vals{i}(1:max_dim,7); % Amps
h(:,i) = vals{i}(1:max_dim,8);% percent
r(:,i) = vals{i}(1:max_dim,9); %deg_Celsius
s(:,i) = vals{i}(1:max_dim,10); %kPa abs
t(:,i) = vals{i}(1:max_dim,11); %kPa abs
u(:,i) = vals{i}(1:max_dim,12);
end
plot_separator=a; %Set plot separator here
plot_separator=unique(plot_separator);
%-----------------------------------------------Begin Plots---------------------------------
%Plot Markers:
markers='so*x+d^v.><ph';
%-----Plot ---------------
%Set Variable of Interest:
y_plot=h;
x_plot=a;
%------------
y_plot_mean=zeros(length(plot_separator),numfiles);
y_plot_std=zeros(length(plot_separator),numfiles);
%the following loop separates plots by seperator
for k=1:length(plot_separator);
for i=1:numfiles
if mean(a(1,i))==plot_separator(k)
y_plot_mean(k,i)=mean(y_plot(1:vals_length(1,i),i));
y_plot_std(k,i)=std(y_plot(1:vals_length(1,i),i));
end
end
end
%To not plot zeros:
y_plot_mean(y_plot_mean==0)=nan;
%find index to first column with actual values (not nan):
col = zeros(length(plot_separator),1);
use_col = zeros(length(plot_separator),1);
for i=1:length(plot_separator)
if all(isnan(y_plot_mean(i,:)))==0;
col(i) = find(sum(~isnan(y_plot_mean(i,:)),1) > 0, 1 ,'first');
use_col(i) = 1; %only rows that are not empty will be used
end
end
figure('visible','on');
plot_set=zeros(length(plot_separator),1);
legend_cell=cell(length(plot_separator),1);
for i=1:length(plot_separator)
if use_col(i)==1
plot_set(i,1)=errorbar(x_plot(1,col(i):end),y_plot_mean(i,col(i):end),y_plot_std(i,col(i):end));
set(plot_set(i,1),'Marker',markers(i));
legend_cell{i,1}={['/Phi=' num2str(plot_separator(i))]};
legend_cell{i,1}=num2str(cell2mat(legend_cell{i,1}));
hold all;
end
end
legend(legend_cell{2:4});
xlabel('Spark Advance (CAD BTC)');
ylabel('Throttle (%)');
0 Comments
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Labels and Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!