In an assignment A(I) = B, the number of elements in B and I must be the same.

1 次查看(过去 30 天)
Hello, I'm getting the following error:
  • * ***COMMAND WINDOW***** * *In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in C_B_HW_9 (line 22) Comfort(count)=('cold');
for count=1:1:20
C(count) = -50+200*rand(1);
K(count) = C(count)+273.15;
F(count) = C(count)*9/5+32; %converting celsius to fahrenheit
R(count) = C(count)*1.8+491.67;
if F(count)>32
if F(count)>55
if F(count)>70
if F(count)>90
if F(count)>105
Comfort(count)=('!!!');
else
Comfort(count)=('hot');
end
else
Comfort(count)=('warm');
end
else
Comfort(count)=('mild');
end
else
Comfort(count)=('cold');
end
else
Comfort(count)=('freeze');
end
end
for count=1:20
fprintf('\n %.0f %.0f %.1f %.2f %.1f %s\n', count,C(count), K(count), F(count), R(count), Comfort(count));
end

采纳的回答

Walter Roberson
Walter Roberson 2014-3-23
编辑:Walter Roberson 2014-3-23
strings are vectors of characters, not indivisible units. If your string is 4 characters long then you need to store it into 4 locations.
You may wish to consider using cell arrays.
Comfort{count} = 'mild';
notice the {} instead of (). Same goes when you want to retrieve, Comfort{count}

更多回答(1 个)

Joseph Cheng
Joseph Cheng 2014-3-23
each of your "Comfort(count)= something" is trying to stuff the string that has 3+ elements into a single array index. Perhaps if you use the {} instead of () to make these cells. example: Comfort{count}='!!!'; This will allow different length strings to be in Comfort for each iteration of count.
however at the bottom in the fprintf you may need to use "cell2mat(Comfort(count))"
  1 个评论
Joseph Cheng
Joseph Cheng 2014-3-23
the elseif will also compact your nested if statements
if Temp>105
Comfort{i}= '!!!'
elseif Temp>90
Comfort{i} = 'hot'
elseif Temp>70
Comfort{i} = 'warm'
elseif Temp>50
Comfort{i} = 'mild'
else
Comfort{i} = 'cold'
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by