Error using surf (line 71) Z must be a matrix, not a scalar or vector.
8 views (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 30 Dec 2024
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
Answers (1)
Torsten
on 30 Dec 2024
Edited: Torsten
on 30 Dec 2024
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 Comments
Walter Roberson
on 3 Jan 2025
You do not have to pre-allocate FL_hat... it is just a good idea.
FL_hat = zeros(numel(beta), numel(alpha));
See Also
Categories
Find more on Annotations 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!