Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Assignment of a string to an empty matrix

1 visualización (últimos 30 días)
Matt Rulli
Matt Rulli el 16 de Abr. de 2018
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
I have a script that's supposed to assign a random name from a list. The script calls on a function to pick the name, and the function works fine. When I try to assign the name, it gives me the following error: "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side." The problem is in the first FOR loop under the %% Pick Teams header: Teams{aa}(bb) = new_name; Here's my whole script>>
filename = input('Enter student list file name:','s');
team_size = input('Enter number of students per team:');
%%Import Data
names = textread(filename,'%s %*s','headerlines',1);
%%Pre-allocate Teams cell
n_names = length(names);
n_teams = round(n_names/team_size);
Teams = cell(n_teams,1);
%%Pick Teams
for aa = 1:team_size
for bb = 1:team_size
new_name = pick_names(names);
Teams{aa}(bb) = new_name;
end
end
%%Team for Extras
n_extras = rem(n_names,team_size);
for aa = 1:n_extras
new_name = pick_names(names);
Teams{n_teams+1}(aa) = new_name;
end
%%Display Teams
for aa = 1:length(names)
fprintf('Members of Team %d: \n',aa)
disp(char(Teams{aa}))
end
Any help is greatly appreciated!!
  1 comentario
Bob Thompson
Bob Thompson el 16 de Abr. de 2018
If I am not mistaken then your indexing of Teams{aa}(bb) calls a specific element of cell aa within Teams. This is acceptable, but using parentheses indicates that bb by default is a numerical or logical element, and therefore cannot receive a string. Try
Teams{aa}{bb} = new_name;

Respuestas (0)

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by