Related
I'm working in matlab 2016.
I have a program code. It needs to work correctly.
Here is the program:
clear all;
addpath('..');
training_files = dir('cootes/*.bmp.mat');
for i=1:numel(training_files)
load(sprintf('cootes/%s', training_files(i).name));
app = imread(sprintf('cootes/%s', training_files(i).name(1:end-4)));
% Map RGB colors to [0,1]
appearances(:,:,:,i) = double(app) ./ 255;
shapes(:,:,i) = xy2ij(annotations, size(app,1));
end
load('cootes/triangulation.mat');
test_img = 2;
one_out = [1:test_img-1, test_img+1:size(shapes,3)];
AAM = build_model_2d(shapes(:,:,one_out), appearances(:,:,:,one_out), 'triangulation', triangulation);
fprintf('\n******************************************************* 2D FITTING *******************************************************\n\n');
disp 'Figure 1: leave-one-out fitting result (red mesh) using as intialization a random shape from the training set (blue mesh).'
disp 'Figure 2: reconstructed appearance.'
disp 'Usage: Hit a random key to use a different initialization shape. Use CTRL+C to quit.'
fprintf('\n');
while 1
init_shape = one_out(round(rand()*(numel(one_out) - 1) + 1));
try
[ fitted_shape fitted_app ] = fit_2d(AAM, shapes(:,:,init_shape) + repmat([-5 -5], [size(shapes, 1) 1 1]), appearances(:,:,:,test_img), 20);
figure(1)
imshow(appearances(:,:,:,test_img));
hold on;
triplot(AAM.shape_mesh, shapes(:,2,init_shape), shapes(:,1,init_shape), 'b');
triplot(AAM.shape_mesh, fitted_shape(:,2), fitted_shape(:,1), 'r');
hold off;
figure(2)
imshow(fitted_app);
pause;
catch ME
fprintf('Fitting diverged: %s\n', ME.message);
end
end
After starting the program appears the following error:
Undefined function or variable 'shapes'.
Error in annotate_test (line 29)
AAM = build_model_2d(shapes, appearances, 'triangulation', triangulation);
Сan anyone explain why the variable 'shapes' is not defined. Although clearly spelled out:
shapes(:,:,i) = xy2ij(annotations, size(app,1));
Please help, I'm newby and don't understand where can be the error.
training_files = dir('cootes/*.bmp.mat'); %error is here
try this.
training_files = dir(fullfile(mypath,'*.bmp.mat')); % should work if you have .bmp.mat files in that path
I am trying to plot a world map in mollweide projection using geoshow command. However, I am not able to modify the colors in the plot based on cutomized colormap values. Most likely this is an issue with how axesm and geoshow commands are used together, Please help me on this. See the ref. code below:
G = rand(180,360);
G(1:90,:)=-1*G(1:90,:);
R = georasterref('RasterSize',size(G),...
'Latlim',[-90 90], 'Lonlim', [-180 180],'ColumnsStartFrom','north');
% ref this link: http://stackoverflow.com/questions/34727526/matlab-plot-raster-map-with-custom-colormap/34740112#34740112
%setting up graphics parameters
my_colormap = [254 204 92
253 141 60
240 59 32
189 0 38]/255 ;
startval=min(min(G));
endval=max(max(G));
nElements =size(my_colormap,1);
stepSize=(endval-startval)/(nElements-1);
breaks = startval:stepSize:endval;
labels = textscan(num2str(round((breaks*100))/100),'%s') ;
labels = labels{1};
[~,indices] = histc(G,breaks);
%actual graphics
figure
hm=axesm ('mollweid', 'Frame', 'on', 'Grid', 'off');
geoshow(G,R);%geoshow(indices,R);
colormap(my_colormap);
set(gca,'color','none');
set(gca,'box','off','xtick',[],'xcolor','none')
set(gca,'box','off','ytick',[],'ycolor','none')
hc=colorbar('location','southoutside');
caxis([breaks(1) breaks(length(breaks))])%caxis([0 length(breaks)-1])
hcP = [.7 .28 .2 .02];
set(hc,'position',hcP);
h.YTickLabel = labels ;
one of my colleague pointed me to the solution and am posting it here for other's reference:
geoshow(G,R,'DisplayType','texturemap')
I want to merge a line plot with 12 box plot. I can plot both of them in a figure. But the line plot position is not matching with Box plot. Please suggest. Figure is attached
Code:
clc
clear
[data header]=xlsread('Dry and Wet Spell Length.xlsx');
RCP26_JHT=(mean(data(1:4:16,:),1));
RCP26_QIN=mean(data(2:4:16,:),1);
RCP26_SCA=mean(data(3:4:16,:),1);
RCP26_WOL=mean(data(4:4:16,:),1);
RCP45_JHT=mean(data(18:4:33,:),1);
RCP45_QIN=mean(data(19:4:33,:),1);
RCP45_SCA=mean(data(20:4:33,:),1);
RCP45_WOL=mean(data(21:4:33,:),1);
RCP85_JHT=mean(data(35:4:50,:),1);
RCP85_QIN=mean(data(36:4:50,:),1);
RCP85_SCA=mean(data(37:4:50,:),1);
RCP85_WOL=mean(data(38:4:50,:),1);
ANUSPLIN_JHT=mean(data(52,1:23));
ANUSPLIN_QIN=mean(data(53,1:23));
ANUSPLIN_SCA=mean(data(54,1:23));
ANUSPLIN_WOL=mean(data(55,1:23));
AN_Data=repmat([ANUSPLIN_JHT,ANUSPLIN_QIN,ANUSPLIN_SCA,ANUSPLIN_WOL]',1,3);
B=reshape(AN_Data',1,12);
y=1:12;
p=plot(B,'k-s', 'MarkerFaceColor','k');
legend(p,'Obs','Location','north');
hold on
xlim = get(gca,'XLim');
positions=[1,1.25,1.5, 2,2.25,2.5 3 3.25 3.5, 4 4.25,4.5];
boxplot([RCP26_JHT',RCP45_JHT',RCP85_JHT',RCP26_QIN',RCP45_QIN',RCP85_QIN',RCP26_SCA',RCP45_SCA',RCP85_SCA', RCP26_WOL', RCP45_WOL',RCP85_WOL'],'positions', positions);
color = ['b','c','y','b','c','y','b','c','y','b','c','y'];
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),color(j),'FaceAlpha',.5);
end
legend('Obs','RCP 8.5', 'RCP 4.5', 'RCP 2.6');
h1=findobj(gca,'tag','Outliers');
delete (h1);
set(gca,'xtick',[mean(positions(1:3)) mean(positions(4:6)) mean(positions(7:9)) mean(positions(10:12)) ]);
set(gca,'xticklabel',{'JHT','QIN','SCA','WOL'});
set(gca,'XLim',xlim);
hold off
I have a three groups such as
Group 1={x1,x2}
x1=[1,2,2,3,5,3]
x2=[2,5,4,5,8,6]
Group 2={x3,x4}
x3=[2,8,9,2,1,6]
x4=[5,4,3,22,11,6]
Group 2={x5,x6}
x5=[10,12,22,4]
x6=[12,15,4,25]
I want to draw them into char by boxplot function by group. I found the way to resolve it. But it cannot draw. Could you help me please?
x1=[1,2,2,3,5,3];
x2=[2,5,4,5,8,6];
g1={x1,x2};
%group2
x3=[2,8,9,2,1,6];
x4=[5,4,3,22,11,6];
g2={x3,x4};
%group3
x5=[10,12,22,4];
x6=[12,15,4,25];
g3={x5,x6};
G=cat(1,g1,g2,g3);
class={1,2,3}
positions = [1 1.25 2 2.25 3 3.25];
boxplot(G,class, 'positions', positions);
set(gca,'xtick',[mean(positions(1:2)) mean(positions(3:4)) mean(positions(5:6)) ])
set(gca,'xticklabel',{'Group1','Group2','Group3'})
color = ['c', 'y', 'c', 'y'];
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),color(j),'FaceAlpha',.5);
end
c = get(gca, 'Children');
hleg1 = legend(c(1:2), 'Feature1', 'Feature2' );
Your problem is that the grouping variables were constructed in the wrong way. Use this sample:
x1=[1,2,2,3,5,3];
x2=[2,5,4,5,8,6];
g1=[x1,x2];
v1=[repmat('A',1,numel(x1)),repmat('B',1,numel(x2))];
%group2
x3=[2,8,9,2,1,6];
x4=[5,4,3,22,11,6];
g2=[x3,x4];
v2=[repmat('A',1,numel(x3)),repmat('B',1,numel(x4))];
%group3
x5=[10,12,22,4];
x6=[12,15,4,25];
g3=[x5,x6];
v3=[repmat('A',1,numel(x5)),repmat('B',1,numel(x6))];
%%
G=[g1,g2,g3]
vg1 = [repmat('1',1,numel(v1)),repmat('2',1,numel(v2)),repmat('3',1,numel(v3))];
vg2=[v1,v2,v3] ;
%%
clc
boxplot(G', {vg1';vg2'}, 'factorseparator',1 , 'factorgap',30,...
'colorgroup',vg2','labelverbosity','majorminor');
I want to write a script for a candlestick-plot that I can run in Octave as well as in Matlab.
So far I use the following code:
D=[ '15-Jul-2013'
'16-Jul-2013'
'17-Jul-2013'
'18-Jul-2013'
'19-Jul-2013'
'22-Jul-2013'
'23-Jul-2013'
'24-Jul-2013'];
O=[25.93 26.39 26.37 26.75 25.82 25.99 26.10 26.32];
H=[26.43 26.75 26.78 26.77 26.11 26.13 26.30 26.53];
L=[25.60 26.01 26.30 26.12 25.60 25.72 25.97 26.05];
C=[26.28 26.32 26.65 26.18 25.88 26.05 26.13 26.51];
datapoints=length(C);
hold on;
for i=1:datapoints
plot([i i],[L(i) H(i)],'linewidth',2,'Color','k');
if C(i)>O(i)
plot([i i],[O(i) C(i)],'linewidth',5,'color','r');
else
plot([i i],[O(i) C(i)],'linewidth',5,'color','g');
end
end
hold off;
grid on;
xlim([0 datapoints+1]);
y=get(gca,'ylim');
ymin=int16(y(1)-0.5);
ymax=int16(y(2)+0.5);
ylim([ymin ymax]);
XTick=zeros(1,length(datapoints));
j=1;
for i=1:1:datapoints
XTick(j)=i;
j=j+1;
end
set(gca,'XTick',XTick,'XTickLabel','')
pos = get(gca,'Position');
set(gca,'Position',[pos(1), .15, pos(3) .75])
for i=1:length(XTick)
hText = text(XTick(i), double(ymin), D(XTick(i),:));
set(hText,'Rotation',45,'HorizontalAlignment','right','VerticalAlignment','top');
end
The resulting plot looks quite nice in Matlab but looks very terrible in Octave. How can I make the plot looks nice in both programms?
The solution here was to use the fill function instead of plot.
The following code leads to similar results in octave and matlab independent of the plotting interface:
D=[ '15-Jul-2013'
'16-Jul-2013'
'17-Jul-2013'
'18-Jul-2013'
'19-Jul-2013'
'22-Jul-2013'
'23-Jul-2013'
'24-Jul-2013'];
O=[25.93 26.39 26.37 26.75 25.82 25.99 26.10 26.32];
H=[26.43 26.75 26.78 26.77 26.11 26.13 26.30 26.53];
L=[25.60 26.01 26.30 26.12 25.60 25.72 25.97 26.05];
C=[26.28 26.32 26.65 26.18 25.88 26.05 26.13 26.51];
colorDown = 'g';
colorUp = 'r';
colorLine = 'k';
date = (1:length(O))';
% w = Width of body, change multiplier to draw body thicker or thinner
% the 'min' ensures no errors on weekends ('time gap Fri. Mon.' > wanted
% spacing)
w=.1*min([(date(2)-date(1)) (date(3)-date(2))]);
%%%%%%%%%%%Find up and down days%%%%%%%%%%%%%%%%%%%
d=C-O;
l=length(d);
hold on
%%%%%%%%draw line from Low to High%%%%%%%%%%%%%%%%%
for i=1:l
line([date(i) date(i)],[L(i) H(i)],'Color',colorLine, 'linewidth',2)
end
%%%%%%%%%%draw white (or user defined) body (down day)%%%%%%%%%%%%%%%%%
n=find(d<0);
for i=1:length(n)
x=[date(n(i))-w date(n(i))-w date(n(i))+w date(n(i))+w date(n(i))-w];
y=[O(n(i)) C(n(i)) C(n(i)) O(n(i)) O(n(i))];
fill(x,y,colorDown)
end
%%%%%%%%%%draw black (or user defined) body(up day)%%%%%%%%%%%%%%%%%%%
n=find(d>=0);
for i=1:length(n)
x=[date(n(i))-w date(n(i))-w date(n(i))+w date(n(i))+w date(n(i))-w];
y=[O(n(i)) C(n(i)) C(n(i)) O(n(i)) O(n(i))];
fill(x,y,colorUp)
end
datapoints=length(C);
xlim([0 datapoints+1]);
y=get(gca,'ylim');
ymin=int16(y(1)-0.5);
ymax=int16(y(2)+0.5);
ylim([ymin ymax]);
XTick=zeros(1,length(datapoints));
j=1;
for i=1:2:datapoints
XTick(j)=i;
j=j+1;
end
set(gca,'XTick',XTick,'XTickLabel','')
pos = get(gca,'Position');
set(gca,'Position',[pos(1), .15, pos(3) .75])
for i=1:length(XTick)
hText = text(XTick(i), double(ymin), D(XTick(i),:));
set(hText,'Rotation',45,'HorizontalAlignment','right','VerticalAlignment','top');
end
hold off
Here's what I get with Octave 3.6.2 (I have used screen captures to illustrate what it actually looks like on the screen):
Graphics toolkit qt
Graphics toolkit gnuplot
Graphics toolkit fltk
The bodies of the candles are vertical in all 3 cases.