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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Mzo. de 2014
Editada: Walter Roberson el 23 de Mzo. de 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}

Más respuestas (1)

Joseph Cheng
Joseph Cheng el 23 de Mzo. de 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 comentario
Joseph Cheng
Joseph Cheng el 23 de Mzo. de 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

Iniciar sesión para comentar.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by