Error using surf (line 71) Z must be a matrix, not a scalar or vector.

5 vues (au cours des 30 derniers jours)
Noob
Noob le 30 Déc 2024
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 commentaires
Walter Roberson
Walter Roberson le 30 Déc 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
Noob
Noob le 30 Déc 2024
Modifié(e) : Noob le 30 Déc 2024
Hi @Walter Roberson! My little FL_hat function is in a separate function file, where beta doesn't show up.
Beta only shows up in my data-plotting file, where I use it to vary the actual model inputs to FL_hat, and then make figures.
While beta is not an actual model input, it is an important parameter for my work, so I wanted to plot FL_hat against alpha, an actual model input, and beta, not a model input, but an important descriptive parameter.

Connectez-vous pour commenter.

Réponse acceptée

Torsten
Torsten le 30 Déc 2024
Modifié(e) : Torsten le 30 Déc 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 commentaires
Noob
Noob le 3 Jan 2025
Modifié(e) : Noob le 3 Jan 2025
Hi @Walter Roberson! In your nested for loops, do you have to pre-allocate FL_hat, and then write values to it? If so, how can I do so?
Walter Roberson
Walter Roberson le 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));

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by