How to enter variable names into plot title (R2019a)

조회 수: 170 (최근 30일)
Nandakumar Unnikrishnan
Nandakumar Unnikrishnan 2020년 10월 12일
편집: madhan ravi 2020년 10월 12일
Hello,
I am trying to enter a variable name into my plot title and it keeps failing. I have been looking up examples that have worked previously but I think that it is different in this version.
clc; clear all; close all;
x = -10:0.001:10;
% 2D Gaussian
m = [0 1 2 3 4 5]; s = [1 2 3];
for i = 1:5 %When the standard Deviation stays the same
p = (1/(s(1)*sqrt(2*pi))).*exp(-(x-m(i)).^2/(2*(s(1))^2));
figure (i);
plot(x,p);
title('2D Gaussian when mean is' m(i) ' and standard deviation is's(i));
end
This is the code that I have used right now. But Even when its not working. I even tried using [] to encapsulate the title and I tried using %f for the variables, but none of that seems to work. Does anyone have any advise for me?
Thank you

채택된 답변

madhan ravi
madhan ravi 2020년 10월 12일
title(sprintf('2D Gaussian when mean is %.2f and standard deviation is %.2f', m(ii), s(ii)))
  댓글 수: 2
Nandakumar Unnikrishnan
Nandakumar Unnikrishnan 2020년 10월 12일
Index exceeds the number of array elements (3).
Error in Project3 (line 9)
title(sprintf('2D Gaussian when mean is %.2f and standard deviation is %.2f', m(i), s(i)));
I got this error message when I tried this. I also tried m(ii) and s(ii), but MATLAB did not recognize it
madhan ravi
madhan ravi 2020년 10월 12일
편집: madhan ravi 2020년 10월 12일
title(sprintf('2D Gaussian when mean is %d and standard deviation is %d', m(i), s(1)));

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

추가 답변 (1개)

Bruno Luong
Bruno Luong 2020년 10월 12일
title("2D Gaussian when mean is " + m(i) + " and standard deviation is " + s(i))
  댓글 수: 2
Nandakumar Unnikrishnan
Nandakumar Unnikrishnan 2020년 10월 12일
Index exceeds the number of array elements (3).
Error in Project3 (line 9)
title("2D Gaussian when mean is " + m(i) + " and standard deviation is " + s(i));
I got this error message when I tried this code. I also tried ' instead of ", but that also did not work
Bruno Luong
Bruno Luong 2020년 10월 12일
check what are m, s, i, title in YOUR code

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

카테고리

Help CenterFile Exchange에서 Digital Filter Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by