What changes should i have to make in my code to remove the error ("Error using contourf Z must be at least a 2x2 matrix.")
8 ビュー (過去 30 日間)
古いコメントを表示
%------Creating a strain matrix-----------%
E = zeros(2*(size(K1,1)-1),2*(size(K1,1)-1))
for j = 1: (size(K1,1)-1)
for i = 1: (size(K1,1)-1)
v = [zpk1(j,i);zpk2(j,i);zpk1(j,i+1);zpk2(j,i+1);zpk1(j+1,i+1);zpk2(j+1,i+1);zpk1(j+1,i);zpk2(j+1,i)];
e1= strain1(v);
e2= strain2(v);
e3= strain3(v);
e4= strain4(v);
E((2*j-1),(2*i-1)) = e1(1);
E((2*j-1),2*i) = e2(1);
E(2*j,(2*i-1)) = e4(1);
E(2*j,2*i) = e3(1);
end
end
su = round(4*d/g)+1 ;
r = E((((su-1)- round(d/g)):-1:1),(su-1));
p = E((((su-1)- round(d/g)):-1:1),(su));
st = (p+r)./(1.5*(10)^(-4));
m = g*(size(r)-1);
H = 0:g:(m);
h = H./10;
writematrix(st)
type 'st.txt';
writematrix(h)
type 'h.txt';
plot(h,st)
xlim([0 1.5])
xlabel("X2/d",'fontsize',14)
ylabel("Normalised Strain along vertical path", 'fontsize',13)
legend("d/w = 0.5")
saveas(gcf,'plot.png')
contourf(st)
colorbar
saveas(gcf,'contour.png')
1 件のコメント
採用された回答
Garmit Pant
2024 年 6 月 19 日
Hello Mohd Babar
From what I understand, you are trying to construct a strain matrix ‘E’ from given variables (‘zpk1’, ‘zpk2’, etc.), and then process this matrix to extract specific data for plotting and analysis.
To resolve the error you are encountering, please ensure that the data you're working with is suitable for a contour plot.
Please refer to the following steps to resolve the issue at hand:
1) Check Data Dimensions:
- “contourf” function needs at least a 2x2 matrix. If ‘st’ is a vector, it won't work for “contourf”. Use “size(st)” function to check its dimensions.
- The calculation of ‘st’ uses the calculated variables ‘p’ and ‘r’. Given the definition of ‘p’ and ‘r’ is as follows:
r = E((((su-1)- round(d/g)):-1:1),(su-1));
p = E((((su-1)- round(d/g)):-1:1),(su));
- Using the above definition, given that ‘E’ is a 2-dimensional matrix, the variables ‘p’ and ‘r’ will have 1-D column vectors as their value. Thus ‘st’ which is defined as follows will also represent a 1-D vector:
st = (p+r)./(1.5*(10)^(-4));
2) Ensure Two-Dimensional Data:
- Contour plots represent variations of a quantity in two-dimensional space. Please ensure that ‘st’ represents as a two-dimensional field.
- For one-dimensional data, the line plot “plot(st)” is more suitable.
3) Debugging Tips:
- Print the shape of ‘st’ before attempting to use “contourf” to confirm it meets the 2x2 minimum requirement.
- Review the computation steps leading to ‘st’ to identify where dimensionality might be getting reduced.
If ‘st’ is inherently one-dimensional, reconsider the use of “contourf”. For input data that's incorrectly shaped, adjust your data preparation strategy to ensure ‘st’ is a 2x2 matrix or larger.
I hope you find the above explanation and suggestions useful!
2 件のコメント
その他の回答 (1 件)
Walter Roberson
2024 年 6 月 19 日
su = round(4*d/g)+1 ;
Not enough information is given to be sure, but it looks like su is a scalar. If it is not a scalar, then either d or g would have to be non-scalar and the / operation would (probably) fail if g is non-scalar... so su can only be non-scalar if d is non-scalar but g is scalar. Not impossible, but seems unlikely.
r = E((((su-1)- round(d/g)):-1:1),(su-1));
p = E((((su-1)- round(d/g)):-1:1),(su));
We see further evidence that su is likely scalar: it is used inside the colon operator, and the colon operator would ignore all except the first entry if su were a non-scalar.
With su being scalar, you are using a vector of indices for the first dimension and a scalar index for the second dimension, so r and p are both going to be column vectors ( not 2D arrays).
st = (p+r)./(1.5*(10)^(-4));
Column vector plus column vector gives column vector, not 2D array.
contourf(st)
contourf() of a column vector is going to fail because contourf() requires a 2D array.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Contour Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!