Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Assignment of a string to an empty matrix

1 vue (au cours des 30 derniers jours)
Matt Rulli
Matt Rulli le 16 Avr 2018
Clôturé : MATLAB Answer Bot le 20 Août 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 commentaire
Bob Thompson
Bob Thompson le 16 Avr 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;

Réponses (0)

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by