Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

matlab - How to use "csvread" when the contents in the file have different formats?

I have a .csv file and the format is shown below:

mapping.csv

5188.40811,TMobileML
5131.40903,TMobileGregsapt
5119.40791,TMobileJonsapartment
5123.40762,TMobileRedhat

i want to store it in an 4 by 2 array, when i have a value such as 5131.40903(this is a 'string' not 'int'), i want to find the mapping relation which is TMobileGregsapt. But i meet two problem, the first is i can't use csvread('mapping.csv'), it will have some error: (I think the problem might be 5131.40903 will be int when i use csvread, but TMobileGregsapt is a string...)

??? Error using ==> dlmread at 145
Mismatch between file and format string.
Trouble reading number from file (row 1, field 2) ==> TMobi

Error in ==> csvread at 52
    m=dlmread(filename, ',', r, c);

even though i use dlmread('cell4.csv', ','), it still have some error:

??? Error using ==> dlmread at 145
Mismatch between file and format string.
Trouble reading number from file (row 1, field 2) ==> TMobi

The second problem is how can i finding the mapping relation in easy way, the naive method is using a forloop to find the position of array.

Thanks for your help:)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Both csvread and dlmread only work for numeric data. Something like this should work for you

out=textread('tmp.csv', '%s', 'whitespace',',');
nums =  out(1:2:end);
strs =  out(2:2:end);
% find index of 'TMobileGregsapt'
ind = find(strcmp('TMobileGregsapt',strs));
nums(ind)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...