Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何使用;csvread“;当文件中的内容具有不同的格式时?_Matlab_File Io_Csv - Fatal编程技术网

Matlab 如何使用;csvread“;当文件中的内容具有不同的格式时?

Matlab 如何使用;csvread“;当文件中的内容具有不同的格式时?,matlab,file-io,csv,Matlab,File Io,Csv,我有一个.csv文件,格式如下所示: mapping.csv 5188.40811,TMobileML 5131.40903,TMobileGregsapt 5119.40791,TMobileJonsapartment 5123.40762,TMobileRedhat 我想把它存储在一个4×2的数组中,当我有一个值,比如5131.40903(这是一个“字符串”而不是“int”),我想找到映射关系,它是TMobileGregsapt。但是我遇到了两个问题,第一个是我不能使用csvread('m

我有一个.csv文件,格式如下所示:

mapping.csv

5188.40811,TMobileML
5131.40903,TMobileGregsapt
5119.40791,TMobileJonsapartment
5123.40762,TMobileRedhat
我想把它存储在一个4×2的数组中,当我有一个值,比如
5131.40903
(这是一个“字符串”而不是“int”),我想找到映射关系,它是
TMobileGregsapt
。但是我遇到了两个问题,第一个是我不能使用
csvread('mapping.csv')
,它会有一些错误: (我认为问题可能是
5131.40903
当我使用csvread时将是
int
,但是
TMobileGregsapt
是一个字符串…)

即使我使用了
dlmread('cell4.csv',',')
,它仍然有一些错误:

??? Error using ==> dlmread at 145
Mismatch between file and format string.
Trouble reading number from file (row 1, field 2) ==> TMobi
第二个问题是如何简单地找到映射关系,最简单的方法是使用forloop来找到数组的位置


感谢您的帮助:)

csvread和dlmread都只适用于数字数据。像这样的东西应该适合你

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)

如果您有混合文本/数字csv,但您不知道其格式是什么,或者它是异构的,那么另一个答案将有效,请从以下位置使用“csv2cell”函数:


c(1,:)
将给出标题,
c(2:end,k)
将给出每个列(无标题),
对于k=1:size(c,2)

NUM和
STR的类型是什么?字符串还是字符数组?因为我不能使用str2num(nums)
。。。
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)
c = csv2cell( 'yourFile.csv', 'fromfile');