フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Assignment of a string to an empty matrix

1 回表示 (過去 30 日間)
Matt Rulli
Matt Rulli 2018 年 4 月 16 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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 件のコメント
Bob Thompson
Bob Thompson 2018 年 4 月 16 日
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;

回答 (0 件)

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by