将复杂的CSV格式读入Matlab
我有一个以上格式的文本文件。前3列是标签,需要提取到另一个文件中,以保持订单的完整性 在文件中,每行都有不同数量的标签。我一直在阅读Matlab中的csvread函数,但这是通用的,在上述情况下不起作用 接下来,我需要提取将复杂的CSV格式读入Matlab,matlab,csv,matrix,Matlab,Csv,Matrix,我有一个以上格式的文本文件。前3列是标签,需要提取到另一个文件中,以保持订单的完整性 在文件中,每行都有不同数量的标签。我一直在阅读Matlab中的csvread函数,但这是通用的,在上述情况下不起作用 接下来,我需要提取 4,7,33 308:0.364759856031284 1156:0.273818346738286 1523:0.17279792082766 9306:0.243665855423149 7,4,33 1156:0.185729429759684 1681:0.1
4,7,33 308:0.364759856031284 1156:0.273818346738286 1523:0.17279792082766 9306:0.243665855423149
7,4,33 1156:0.185729429759684 1681:0.104443202690279 5351:0.365670526234034 6212:0.0964006003127458
这样,在矩阵中行1的列308处,我输入了值0.364759856031284,,因为行格式发生了变化,所以需要逐行读取文件 注意,上面的代码未经测试,因此它将包含错误,但应该很容易修复。
我故意省略了一些小部分,填写起来应该不会太难 欢迎来到堆栈溢出!我冒昧地把你的问题格式化了一点。你会发现,关注语法和格式等小问题将有助于你获得更高质量的答案!谢谢你的邀请。:)谢谢你。我决定写一个C++脚本,读取每个行,然后根据分隔符分割文本。我现在将尝试您的代码,并为此感谢您。
308:0.364759856031284 1156:0.273818346738286 1523:0.17279792082766 9306:0.243665855423149
fid = fopen(filename, 'rt'); % open file for text reading
resultMatrix = []; % start with empty matrix
while 1
line = fgetl(fid);
if ~ischar(line), break, end
% get the value separators, :
seps = strfind(line, ':');
% get labels
whitespace = iswhite(line(1:seps(1)-1)); % get white space
lastWhite = find(whitespace, 'last'); % get last white space pos
labelString = deblank(line(1:lastWhite)); % string with all labels separated by ,
labelString(end+1) = ','; % add final , to match way of extraction
ix = strfind(labelString, ','); % get pos of ,
labels = zeros(numel(ix),1); % create the labels array
start = 1;
for n1 = 1:numel(labels)
labels(n1,1) = str2num(labelString(start:ix(n1)-1));
end
% continue and do a similar construction for the COL:VALUE pairs
% If you do not know how many rows and columns your final matrix will have
% you need to continuously update the size 1 row for each loop and
% then add new columns if any of the current rows COLs are outside current matrix
cols = zeros(numel(seps,1);
values = cols;
for n1 = 1:numel(seps)
% find out current columns start pos and value end pos using knowledge of
% the separator, :, position and that whitespace separates COL:VALUE pairs
cols(n1,1) = str2num(line(colStart:seps(n1)-1));
values(n1,1) = str2num(line(seps(n1)+1:valueEnd));
end
if isempty(resultMatrix)
resultMatrix = zeros(1,max(cols)); % make the matrix large enough to hold the last col
else
[nR,nC] = size(resultMatrix); % get current size
if max(cols) > nC
% add columns to resultMatrix so we still can fit column data
resultMatrix = [resultMatrix, zeros(nR, max(cols)-nC)];
end
% add row
resultMatrix = [resultMatrix;zeros(1,max(nC,max(cols)))];
end
% loop through cols and values and populate the resultMatrix with wanted data
end
fclose(fid);