Matrix dimensions must agree
Show older comments
Hello i am trying to use 'pcolor' command and error keeps popping up that ' matrix dimensions must agree ' i am a beginner and clueless on how to fix this if anyone could help id appreciate it , Thank you
ls *.asc
load('CTDGrpA1_conv_binav_asc.asc')
load('CTDGrpA2_conv_binav_asc.asc')
load('CTDGrpA3_conv_binav_asc.asc')
load('CTDGrpA4_conv_binav_asc.asc')
load('CTDGrpA5_conv_binav_asc.asc')
load('CTDGrpA6_conv_binav_asc.asc')
load('CTDGrpA7_conv_binav_asc.asc')
load('CTDGrpA8_conv_binav_asc.asc')
load('CTDGrpA9_conv_binav_asc.asc')
T1=CTDGrpA1_conv_binav_asc(:,2);S1=CTDGrpA1_conv_binav_asc(:,3);depth1=CTDGrpA1_conv_binav_asc(:,1);
T2=CTDGrpA2_conv_binav_asc(1:13,2);S2=CTDGrpA2_conv_binav_asc(1:13,3);depth2=CTDGrpA2_conv_binav_asc(1:13,1);
T3=CTDGrpA3_conv_binav_asc(1:14,2);S3=CTDGrpA3_conv_binav_asc(1:14,3);depth3=CTDGrpA3_conv_binav_asc(1:14,1);
T4=CTDGrpA4_conv_binav_asc(1:14,2);S4=CTDGrpA4_conv_binav_asc(1:14,3);depth4=CTDGrpA4_conv_binav_asc(1:14,1);
T5=CTDGrpA5_conv_binav_asc(1:15,2);S5=CTDGrpA5_conv_binav_asc(1:15,3);depth5=CTDGrpA5_conv_binav_asc(1:15,1);
T6=CTDGrpA6_conv_binav_asc(1:12,2);S6=CTDGrpA6_conv_binav_asc(1:12,3);depth6=CTDGrpA6_conv_binav_asc(1:12,1);
T7=CTDGrpA7_conv_binav_asc(1:11,2);S7=CTDGrpA7_conv_binav_asc(1:11,3);depth7=CTDGrpA7_conv_binav_asc(1:11,1);
T8=CTDGrpA8_conv_binav_asc(1:8,2);S8=CTDGrpA8_conv_binav_asc(1:8,3);depth8=CTDGrpA8_conv_binav_asc(1:8,1);
T9=CTDGrpA9_conv_binav_asc(1:5,2);S9=CTDGrpA9_conv_binav_asc(1:5,3);depth9=CTDGrpA9_conv_binav_asc(1:5,1);
max_length=29;
Temperature1=[T1; nan(max_length-length(T1),1)];
Temperature2=[T2; nan(max_length-length(T2),1)];
Temperature3=[T3; nan(max_length-length(T3),1)];
Temperature4=[T4; nan(max_length-length(T4),1)];
Temperature5=[T5; nan(max_length-length(T5),1)];
Temperature6=[T6; nan(max_length-length(T6),1)];
Temperature7=[T7; nan(max_length-length(T7),1)];
Temperature8=[T8; nan(max_length-length(T8),1)];
Temperature9=[T9; nan(max_length-length(T9),1)];
whos Temperature*
Temperature_Matrix=[Temperature1 Temperature2 Temperature3 Temperature4 Temperature5 Temperature6 Temperature7 Temperature8 Temperature9];
Depth=depth5;
station=1:9;
figure
pcolor(station,Depth,Temperature_Matrix)
Answers (1)
Read the descriptions on the pcolor documentation page for X, Y, and C - specifically how their sizes must be related.
Then check the sizes of your variables station, Depth, and Temperature_Matrix, figure out why they don't conform to what the documentation says and what you need to do about it.
It looks like your Temperature_Matrix is a matrix with 9 columns, and station is a vector with 9 elements. Those are consistent with each other, so the problem is with Depth (i.e., depth5). It's a 15x1 column vector, but Temperature_Matrix has 29 (=max_length) rows, so that's inconsistent.
1 Comment
Rather than hard-coding fixed variable values like 29 into your code, determine what you need from the actual variables you have. Here's how you could build Temperature_Matrix from T1 through T9, where the number of rows in Temperature_Matrix is the maximum number of elements in T1 through T9, and also to pick the depth vector with the maximum number of elements, to use in pcolor:
T = {T1,T2,T3,T4,T5,T6,T7,T8,T9};
Z = {depth1,depth2,depth3,depth4,depth5,depth6,depth7,depth8,depth9};
N = numel(T);
T_length = cellfun(@numel,T);
[max_length,max_idx] = max(T_length);
Temperature_Matrix = NaN(max_length,N);
for ii = 1:N
Temperature_Matrix(1:T_length(ii),ii) = T{ii};
end
Depth = Z{max_idx};
station = 1:N;
figure
pcolor(station,Depth,Temperature_Matrix)
Categories
Find more on Graph and Network Algorithms 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!