tags:

views:

162

answers:

3

I have managed to input a number data file into a matrix but have been unable to do so for any data that is not a number.

I have a list of 47 names and supposed to generate a random name from the list. I have tried to use the function textscan but was not going anywhere. Also how do I generate a random name from the list? All I have been able to do was generate a random number between 1 to 47.


Appreciate the replies. I should have said I need it in MATLAB sorry.

Here is a sample list of data in my data file

name01
name02
name03

and the code to read it:

fid = fopen('names.dat','rt');
headerChars = fgetl(fid);
data = fscanf(fid,'%f,%f,%f,%f',[4 47]).';
fclose(fid);

The above is what I have to read the data file into a matrix but it is only reading the first line. (Yes it was modified from a previous post here on this forums :/)

+1  A: 

Sounds like you're halfway home. Take that random number and use it as an index for the list.

For example, if you randomly generate the number 23 then fetch the 23rd entry in the list which gives you a random name draw.

Paul Sasik
A: 

Use the RANDOMBETWEEN function to get a random number within your range. Use INDEX to get the actual cell value. For instance:

=INDEX(A1:A47, RANDBETWEEN(1, 47))

The above will work for your specific case of 47 names, assuming they're in column A. In general, you'd want something like:

=INDEX(MyNames, RANDBETWEEN(ROW(MyNames), ROW(MyNames) + ROWS(MyNames) - 1))

This assumes you've named your range of cells "MyNames" (for example, by selecting all the cells in your range and setting a name in the naming box). The above formula works by using the ROW function to return the top row of the MyNames array and the ROWS function to get the total rows in MyNames.

Philip Su
I'm guessing that's in Excel? I think the OP wanted a MATLAB solution.
gnovice
Ya... sorry I should have specified about the matlab part. Thanks for the reply
New2Matlab
+3  A: 

Edit: As per the helpful comments from mtrw, and the fixed formatting of the sample data file, I've updated my answer with more detail.

With a single name (i.e. "Bob", "Bob Smith", or "Smith, Bob") on each line of the file, you can use the function TEXTSCAN by specifying '%s' as the format argument (to denote reading a string) and the newline character '\n' as the 'Delimiter' (the character that separates the strings in the file):

fid = fopen('namefile.txt','r');
names = textscan(fid,'%s','Delimiter','\n');
fclose(fid);

Then it's a matter of randomly picking one of the names. You can use the function RANDI to generate a random integer in the range from 1 to the number of names read from the file (found using the NUMEL function):

names = names{1};  %# Get the contents from the cell returned by TEXTSCAN
selectedName = names{randi(numel(names))};
gnovice
@gnovice - man, you type fast.@New2Matlab - The only thing I'd add to gnovice's answer is if your file looks like: Alice Jones Bob Smith Charlie Brownthen change the textscan line to names = textscan(fid, '%s %s', 'CollectOutput', 1);and split the selection into: idx = randi(length(names)); selectedName = {names{idx,:}};
mtrw
Thanks. If you don't mind me asking gnovice, may I know what Delimiter does?
New2Matlab
@New2Matlab - the 'Delimiter' option allows you to specify the characters that appear between fields. For instance, if your file has each name on a separate line, you can use 'Delimiter', '\n'. Strictly speaking, you don't need the delimiter option gnovice used because whitespace is the default delimiter. See http://www.mathworks.com/access/helpdesk/help/techdoc/ref/textscan.html for more information.
mtrw

related questions