Matlab由于某些原因不会绘制我的函数

时间:2019-03-06 22:47:18

标签: matlab plot matlab-figure

我在课堂上有一个作业,并且能够完成大部分作业,但这给我带来了一些麻烦。如果我手动将代码复制并粘贴到命令窗口中,则可以运行和绘制所有内容,但是如果仅调用该函数,它将提供输出,但由于某些原因将无法绘制。有什么想法吗?

我还尝试了我的朋友代码,在我看来,它几乎是一样的,除了出于某种原因,他允许他调用它,然后它会绘图。今天,我已经花了6个小时做这小部分工作,对您的帮助非常感谢。

谢谢!

function [totalheat1,totalheat2] = dheat(Tday,Tnight)


foot=10*sum('Todd');
height=9; %ft
Ctm=0.35; %BTU/lb degF 
mt= 20000; %lbs
A= foot + 4*((sqrt(foot)*height)); %surface area of house (ft^2)
R=25; %degF*ft^2*hour/BTU
To=30; %degF

dt=0.1;
t=dt:dt:24;
n=24/dt;
Td=Tday;
Tn=Tnight;

%regime 1
for i=1:n
  Q1(i)=dt*((A*(Td-To))/(R));  
end

%regime 2

therm=[1:240];
therm(1:70)=Tn;
therm(71:100)=Td;
therm(101:160)=Tn;
therm(161:230)=Td;
therm(231:240)=Tn;

Td=therm(1);

for i=1:n
   Q=dt*((A*(Td-To))/(R));
   chgT=(Q)/(Ctm*mt);
   Ti=Td-chgT;
   if Ti< therm(i)
       Q=(therm(i)-Ti)*(Ctm*mt);
       if Q<3000
           F(i)=Q;
           temp(i)=Td;
       else
          F(i)=3000;
          temp(i)=Ti+((3000)/(Ctm*mt));

       end
   else
       F(i)=0;
       temp(i)=Ti;
   end
   Td=temp(i);
end


totalheat1=sum(Q1);
totalheat2=sum(F);

figure(1)
plot(t,temp)

figure(2)
plot(t,Q1,t,F)

end

1 个答案:

答案 0 :(得分:1)

首先,请确保在调用函数之前打close all force。 这将关闭所有打开的(但可能不可见)的图形。

接下来,此代码段明确告诉要使用的图形和轴:

f1 = figure();
ax1 = axes(f1);
p1 = plot(ax1, t,temp);

f2 = figure();
ax2 = axes(f2);
p2 = plot(ax2, t, Q1, t, F);

如果仍然不起作用: 尝试将包含struct的{​​{1}}传递到这些图形/轴作为函数的第三个参数。在脚本中:

handles

和功能:

s = struct();
s.f1 = figure();
s.ax1 = axes(f1);

s.f2 = figure();
s.ax2 = axes(f2);

dheat(Tday,Tnight,s)

,然后绘图部分将如下所示:

function [s,totalheat1,totalheat2] = dheat(Tday,Tnight,s)

就个人而言,我更喜欢返回计算值并从脚本中调用特定于绘图的内容。 p1 = plot(s.ax1, t,temp); p2 = plot(s.ax2, t, Q1, t, F); p1可以省略。

相关问题