In an assignment A(I) = B, the number of elements in B and I must be the same.
1 view (last 30 days)
Show older comments
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');
- * *This is the script <file:*> * *
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
0 Comments
Accepted Answer
Walter Roberson
on 23 Mar 2014
Edited: Walter Roberson
on 23 Mar 2014
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}
0 Comments
More Answers (1)
Joseph Cheng
on 23 Mar 2014
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 Comment
Joseph Cheng
on 23 Mar 2014
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
See Also
Categories
Find more on String in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!