Error using surf (line 71) Z must be a matrix, not a scalar or vector.
4 次查看(过去 30 天)
显示 更早的评论
Hi there!
I currently have a tiledLayout plot with six tiles; the tiles are plotted against two variables alpha and beta, and I am using the x-axis for alpha, and colors for beta (using a colormap). Now I want to experiment with a 3D surface plot, using the x- and y-axes for alpha and beta. However, when I run the code
[alpha, beta] = meshgrid(alpha,beta);
surf(alpha,beta,FL_hat);
I am getting the error message that FL_hat must be a matrix, not a scalar or vector.
How can I fix this bug?
In my code, I loop through beta, but I set alpha = linspace(0, 2*pi, 1001), for example.
So, I tried not looping through beta, and just setting beta = linspace(0, 2*pi,1001), but that didn't seem to help.
I also tried using the contour3 function, writing:
[alpha, beta] = meshgrid(alpha,beta);
contour3(alpha,beta,FL_hat);
but then I get the error message, "Error using contour3 (line 44) Z must be at least a 2x2 matrix."
Thanks in advance,
2 个评论
Walter Roberson
2024-12-30
The problem is that FL_hat is not a 2D array with at least 2 elements in each of the dimensions.
Looping through beta is not a problem, provided you use a structure similar to
for beta_index = 1 : number_of_betas
current_beta = beta(beta_index);
FL_hat(beta_index, :) = values_calculated_from_current_beta_and_alpha;
end
采纳的回答
Torsten
2024-12-30
编辑:Torsten
2024-12-30
Works for me (assuming that FL_hat is 1001x1001 in your case):
alpha = linspace(0, 2*pi, 51);
beta = linspace(0, 2*pi,21);
FL_hat = rand(21,51);
surf(alpha,beta,FL_hat)
12 个评论
Walter Roberson
2025-1-3
You do not have to pre-allocate FL_hat... it is just a good idea.
FL_hat = zeros(numel(beta), numel(alpha));
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!