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

조회 수: 6 (최근 30일)
Noob
Noob 2024년 12월 30일
댓글: Walter Roberson 2025년 1월 3일
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
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
Noob
Noob 2024년 12월 30일
편집: Noob 2024년 12월 30일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
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
Noob
Noob 2025년 1월 3일
편집: Noob 2025년 1월 3일
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 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by