Why do I keep getting a vertcat error in MATLAB?

12.2k Views Asked by At

For some reason the following code gives me the following error:

Error using CI2 (line 94)
Error using vertcat
Dimensions of matrices being concatenated are not consistent.

The code is:

clear all
% Odds Ratio for no clinically significant change, according to original
% Study

a=[0.48; %Aripiprazole
   0.70; %Olanzapine
   0.66; %Quetiapine added to ADs
   0.57]; %Risperidone

CI=[0.37 0.63;
    0.48 1.02;
    0.51 0.87;
    0.36 0.89]';

figure(1) % Setting the figure to figure 1
clf(1)
plot(1:length(CI),a,'O','markersize', 6)           % plot the mean
hold on;
plot(1:length(CI),CI(1,:),'v','markersize', 6)     % plot lower CI boundary
hold on;
plot(1:length(CI),CI(2,:),'^','markersize', 6)     % plot upper CI boundary
hold on;

for I = 1:length(CI)                               % connect upper and lower bound with a line
line([I I],[CI(1,I) CI(2,I)])
hold on;
end;
xlim([0.5 4.5])

my_labels = ['Aripiprazole'; 'Olanzapine  '; 'Quetiapine1 '; 'Risperidone '];
title('The Efficacy of SGAs as adjuncts in the Treatment of MDD (OR for non-response)')
set(gca,'XTick',[1 2 3 4]);
set(gca,'XTickLabel',my_labels);
xticklabel_rotate([],45)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Odds Ratio for No Clinically Significant Change (CGI)
clear all

a=[0.51; %Aripiprazole
   0.64]; %Quetiapine added to ADs

CI=[0.34 0.78;
    0.49 0.84]';

figure(2) % Setting the figure to figure 2
clf
plot(1:length(CI),a,'O','markersize', 6)           % plot the mean
hold on;
plot(1:length(CI),CI(1,:),'v','markersize', 6)     % plot lower CI boundary
hold on;
plot(1:length(CI),CI(2,:),'^','markersize', 6)     % plot upper CI boundary
hold on;

for I = 1:length(CI)                               % connect upper and lower bound with a line
line([I I],[CI(1,I) CI(2,I)])
hold on;
end
xlim([0.75 2.25])

my_labels = ['Aripiprazole '; 'Quetiapine-1 '];
title('The Efficacy of SGAs as adjuncts in the Treatment of MDD (OR for non-response [CGI])')
set(gca,'XTick',[1 2]);
set(gca,'XTickLabel',my_labels);
xticklabel_rotate([],45)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Odds Ratio for number of participants WITHOUT a remission
clear all

a=[0.48; %Aripiprazole
   0.60; %Olanzapine
   0.64; %Quetiapine
   0.39];

CI=[0.37 0.63;
    0.51 1.04
    0.49 0.84
    0.22 0.69]';

figure(3) % Setting the figure to figure 3
clf
plot(1:length(CI),a,'O','markersize', 6)           % plot the mean
hold on;
plot(1:length(CI),CI(1,:),'v','markersize', 6)     % plot lower CI boundary
hold on;
plot(1:length(CI),CI(2,:),'^','markersize', 6)     % plot upper CI boundary
hold on;

for I = 1:length(CI)                               % connect upper and lower bound with a line
line([I I],[CI(1,I) CI(2,I)])
hold on;
end
xlim([0.5 4.5])

my_labels = ['Aripiprazole '; 'Olanzapine'; 'Quetiapine '; 'Risperidone'];
title('The Efficacy of SGAs as adjuncts in the Treatment of MDD (OR for non-response [CGI])')
set(gca,'XTick',[1 2 3 4]);
set(gca,'XTickLabel',my_labels);
xticklabel_rotate([],45)
1

There are 1 best solutions below

2
On BEST ANSWER

As I mentioned in your other question, the labels are character arrays. You can only concatenate arrays of the same size.

So

['dog';..
 'cat']

is ok, but

['banana';...
'house']

is not.

You have to add spaces to make sure that all of your lines are as long as the longest entry.

['banana';
 'house ';]

As I alluded to in the other question, and as Daryl suggested, you can use a cell array:

my_labels = {'Aripiprazole','Olanzapine','Quetiapine','Risperidone'};

Notice how they're commas instead of semi-colons, and note the curly braces instead of square brackets. This is probably easier to do, and it is probably the right way to do it.