本文介绍了如何使用“csvread”当文件中的内容有不同的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个.csv文件,格式如下所示: mapping.csv 5188.40811,TMobileML 5131.40903,TMobileGregsapt 5119.40791,TMobileJonsapartment 5123.40762,TMobileRedhat 我想把它存储在一个4乘2数组,当我有一个值,如 5131.40903 (这是一个字符串'not'int'),我想找到 TMobileGregsapt 的映射关系。但我遇到两个问题,第一是我不能使用 csvread('mapping.csv'),它会有一些错误:问题可能是 5131.40903 将会 int 当我使用csvread,但 TMobileGregsapt 是一个字符串...) ?使用==>时出错dlmread at 145 文件和格式字符串之间的不匹配。 从文件读取数字的问题(行1,字段2)==> TMobi 错误==> csvread at 52 m = dlmread(filename,',',r,c);即使我使用 dlmread('cell4.csv',','),还是 ,它仍然有一些错误: ?使用==>时出错dlmread at 145 文件和格式字符串之间的不匹配。 从文件读取数字的问题(行1,字段2)==>第二个问题是如何以简单的方式找到映射关系,天真的方法是使用解决方案 csvread和dlmread只适用于数值数据。 out = textread('tmp.csv','%s','whitespace' ','); nums = out(1:2:end); strs = out(2:2:end); %find'TMobileGregsapt' ind = find(strcmp('TMobileGregsapt',strs)); nums(ind) I have a .csv file and the format is shown below:mapping.csv5188.40811,TMobileML5131.40903,TMobileGregsapt5119.40791,TMobileJonsapartment5123.40762,TMobileRedhati 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 145Mismatch between file and format string.Trouble reading number from file (row 1, field 2) ==> TMobiError 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 145Mismatch between file and format string.Trouble reading number from file (row 1, field 2) ==> TMobiThe 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:) 解决方案 Both csvread and dlmread only work for numeric data. Something like this should work for youout=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) 这篇关于如何使用“csvread”当文件中的内容有不同的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-15 06:40