how to allow empty inputs in for loop?

조회 수: 3 (최근 30일)
Arry Rahmadi
Arry Rahmadi 2014년 1월 7일
편집: Arry Rahmadi 2014년 1월 7일
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.

채택된 답변

ES
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
  댓글 수: 1
Arry Rahmadi
Arry Rahmadi 2014년 1월 7일
편집: Arry Rahmadi 2014년 1월 7일
Thanks a lot, choclate warrior! It works. Don't mind my previous comment. I get it now. Again. Thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by