Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Assignment of a string to an empty matrix

1 view (last 30 days)
Matt Rulli
Matt Rulli on 16 Apr 2018
Closed: MATLAB Answer Bot on 20 Aug 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 Comment
Bob Thompson
Bob Thompson on 16 Apr 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;

Answers (0)

This question is closed.

Community Treasure Hunt

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

Start Hunting!