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

5 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by