please help me in loading data to matlab
显示 更早的评论
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.
采纳的回答
更多回答(6 个)
Andrew Newell
2011-2-22
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
2011-2-23
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
2011-2-23
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
2011-2-23
0 个投票
1 个评论
Andrew Newell
2011-2-23
See above for Matt's comment about best practice.
类别
在 帮助中心 和 File Exchange 中查找有关 Live Scripts and Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!