please help me in loading data to matlab
Mostrar comentarios más antiguos
Hi all,
I have some problems in loading file Adult.data into MATLAB. When I try:
>> load adult.data
it displays:
??? Error using ==> load Unknown text on line number 1 of ASCII file C:\Users\Documents\MATLAB\adult.data "Self-emp-not-inc".
A line in the file:
50, Self-emp-not-inc, 83311, Bachelors, 13, Married-civ-spouse, Exec-managerial, Husband, White, Male, 0, 0, 13, United-States, <=50K
I don't know why. I have tried fopen and scan but it is still impossible. Please help me. Thank you so much.
Respuesta aceptada
Más respuestas (6)
Andrew Newell
el 22 de Feb. de 2011
fid = fopen('Adult.data');
A = textscan(fid,'%s','delimiter',',');
fclose(fid)
This reads the data into a cell containing a one-dimensional cell array. The next command will change it to a format that is probably more useful to you (there are 15 fields on each line):
A = reshape(A{:},15,[]);
Matt Tearle
el 23 de Feb. de 2011
BTW, best practice is to accept the answer that solved your initial question, then start a new question for the follow-up. That way, others can follow what's happening (for the benefit of others who might have similar questions). Anyway...
It depends, are you using Stats TB? If not, the base MATLAB way is
nnz((age > 50) & strcmpi(sex,'male'))
I'm assuming age is a numeric array, and sex is a cell array of strings. The > and strcmpi (case-insensitive string comparison) both create logical variables, which are combined using &. Applying the nnz function returns the number of true values.
If you have Stats TB and have the data in a dataset array, with sex as a nominal variable,
nnz((data.age > 50) & (data.sex == 'male'))
Matt Tearle
el 23 de Feb. de 2011
Would you be surprised if I suggested that what you really need is Statistics Toolbox?
But, the brute-force way in MATLAB could be done something like this:
clist = unique(country);
Nctry = length(clist);
num_richbuggers = zeros(Nctry,1);
for k = 1:Nctry
num_richbuggers(k) = nnz(strcmpi(assets,'>50K') & ...
strcmpi(country,clist{k}));
end
love
el 23 de Feb. de 2011
0 votos
love
el 23 de Feb. de 2011
0 votos
1 comentario
Andrew Newell
el 23 de Feb. de 2011
See above for Matt's comment about best practice.
love
el 23 de Feb. de 2011
0 votos
Categorías
Más información sobre Live Scripts and Functions en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!