Reputation: 5
This is my first one:
Assume I have a struct
student_information=struct{'name','','surnames,''};
and two cell arrays
possible_names=cell{'John','Amy','Paul'};
possible_surnames=cell{'Mitchell','Gordon','Maxwell'};
I need to fill the struct field 'name' with random names from the possible_names cell array which i figured:
for i=1:length(student_information)
for j=1:length(possible_names);
student_information(i).name=possible_names(randperm(j,1));
end
end
But i need to fill the struct field 'surnames' with TWO random surnames (i.e "Gordon Maxwell") from the possible_surnames cell array... I tried a simmilar way to the method i used to fill the 'names' field but i didnt.
I really appreciate your help
Upvotes: 0
Views: 63
Reputation: 104555
Your code doesn't make much sense TBH. You have a few syntax errors. Specifically:
student_information=struct{'name','','surnames,''};
'surnames'
needs an ending quotation. This also only allocates one structure. Also, struct
is a function yet you are trying to use it like a cell array. You probably meant this:
student_information=struct('name','','surnames','');
On top of this:
possible_names=cell{'John','Amy','Paul'};
possible_surnames=cell{'Mitchell','Gordon','Maxwell'};
This is invalid MATLAB syntax. cell
is a function, yet you are trying to reference it as if it were a cell
array. Do this instead:
possible_names={'John','Amy','Paul'};
possible_surnames={'Mitchell','Gordon','Maxwell'};
Now, back to your code. Judging from your context, you want to select two random names from possible_surnames
and concatenate these into a single string. Use randperm
but generate two choices instead of 1 like in your code. Next, you can cheat a bit by using sprintf
and separating each name with a space:
possible_surnames={'Mitchell','Gordon','Maxwell'};
names = possible_surnames(randperm(numel(possible_surnames),2));
out = sprintf('%s ', names{:})
out =
Gordon Maxwell
As such, as you're populating your cell array, you can choose one random name from the possible_names
array like so. First you need to allocate the structure properly with a given number of slots:
student_information=struct('name','','surnames','');
student_information=repmat(student_information, 5, 1); %// Make a 5 element structure
possible_names={'John','Amy','Paul'};
for idx = 1 : numel(student_information)
student_information(idx).name = possible_names{randperm(numel(possible_names), 1)};
end
Now for the possible_surnames
, do:
possible_surnames={'Mitchell','Gordon','Maxwell'};
for idx = 1 : numel(student_information)
names = possible_surnames(randperm(numel(possible_surnames),2));
student_information(idx).surnames = sprintf('%s ', names{:});
end
Let's see what this structure looks like now:
cellData = struct2cell(student_information);
fprintf('Name: %s. Surnames: %s\n', cellData{:})
Name: Paul. Surnames: Maxwell Gordon
Name: Amy. Surnames: Mitchell Maxwell
Name: Paul. Surnames: Mitchell Gordon
Name: John. Surnames: Gordon Maxwell
Name: John. Surnames: Gordon Mitchell
Upvotes: 1