how to allow empty inputs in for loop?
3 次查看(过去 30 天)
显示 更早的评论
Hi everyone. I am trying to make a script that takes inputs to calculate thermal resistance of composite walls. I want the script to be able to take empty inputs and replace them with zeros without changing the size of the input matrix, if some of the data are not available. Here's what I tried:
no_plt = input('number of plate =');
A = input('frontal area of plate =');
if isempty(A)
A = 1;
end
k = zeros(1,no_plt);
x = zeros(1,no_plt);
if no_plt == 1
k = input('plate conductivity =');
x = input('plate thickness =');
else
for m = 1:no_plt
k(m) = input (sprintf('conductivity of plate %d =',m));
if isempty (k(m))
k(m) = 0;
end
x(m) = input (sprintf('thickness of plate %d =',m));
if isempty (x(m))
x(m) = 0;
end
end
end
And it failed miserably. Here's the error I got:
In an assignment A(I) = B, the number of elements in B and I must be the same.
I read a very similiar question asked in this forum, but somehow it couldn't help me. And I am not sure I understand the solution either :). Could someone point out where my mistakes are, and explain it to me? I am new to matlab, so please go easy on me if the mistakes are obvious. Thanks in advance.
0 个评论
采纳的回答
ES
2014-1-7
编辑:ES
2014-1-7
In short you cannot do a(1)=[]; where as you can do a temp=[];
try this...
no_plt = input('number of plate =');
A = input('frontal area of plate =');
if isempty(A)
A = 1;
end
k = zeros(1,no_plt);
x = zeros(1,no_plt);
if no_plt == 1
k = input('plate conductivity =');
x = input('plate thickness =');
else
for m = 1:no_plt
temp1 = input (sprintf('conductivity of plate %d =',m));
if isempty (temp1)
k(m) = 0;
else
k(m) = temp1;
end
temp2 = input (sprintf('thickness of plate %d =',m));
if isempty (temp2)
x(m) = 0;
else
x(m)=temp2;
end
end
end
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!