简体   繁体   English

使用octave / matlab中的fscanf将文本文件读入单元格数组

[英]Reading text file using fscanf in octave / matlab into cell array

I have a comma delimited single text file with strings and integers that I'm trying to import into a cell array. 我有一个逗号分隔的单个文本文件,其中包含我想要导入单​​元格数组的字符串和整数。 I then want to export it to several files based on the same Resonance Freq. 然后我想将它导出到基于相同Resonance Freq的几个文件。 and add text to the filename row 并将文本添加到文件名行

This is a sample of the text file to import: (please note the file will be much larger then this) 这是要导入的文本文件的示例:(请注意文件会比这个文件大得多)

Resonance Freq,number,Filename,time,fs,Split-3675,Session Num
277.912902832031250,1,p000001,00:00:01,44100,3675,0
123.912902832031250,2,p000002,00:00:02,44100,3675,2
277.912902832031250,3,p000003,00:00:03,44100,3675,0
277.912902832031250,4,p000001,00:00:01,44100,3675,1
343.912902832031250,5,p000002,00:00:02,44100,3675,0
277.912902832031250,6,p000003,00:00:03,44100,3675,4

And this is the exported text files I want created (text file1) 这是我想要创建的导出文本文件(文本file1)

277.912902832031250,1,/tmp/p000001.wav,00:00:01,44100,3675,0
277.912902832031250,3,/tmp/p000003.wav,00:00:03,44100,3675,0
277.912902832031250,4,/tmp/p000001.wav,00:00:01,44100,3675,1

And this is the exported text files I want created (text file2) 这是我想要创建的导出文本文件(文本文件2)

123.912902832031250,2,/tmp/p000002.wav,00:00:02,44100,3675,2

And this is the exported text files I want created (text file3) 这是我想要创建的导出文本文件(文本文件3)

343.912902832031250,5,/tmp/p000002.wavadded ,00:00:02,44100,3675,0

I'm having a problem with fscanf and using comma delimited data 我遇到fscanf和使用逗号分隔数据的问题

fid = fopen('/tmp/freq_range_color_coded.txt');
m_s = fscanf(fid,'%f %f %s %s %f %f %f');
fclose(fid);

when I access a cell like m_s(1,2) I get back a single letter instead of a field. 当我访问像m_s(1,2)这样的单元格时,我得到一个字母而不是字段。 How can I get it so when I type m_s(1,2) I get back the whole field example 当我输入m_s(1,2)时,我怎么能得到它呢?我回到整个字段的例子

m_s(2,1) should give me 277.912902832031250 m_s(2,1)应该给我277.912902832031250

thanks 谢谢

PS I'm using octave and textscan is not compatible with it. PS我正在使用八度音,而且文本扫描与它不兼容。

Try skipping the header line when importing with textscan: 使用textscan导入时尝试跳过标题行:

fid = fopen('/tmp/freq_range_color_coded.txt');
m_s = textscan(fid,'%f %f %s %s %f %f %f','delimiter', ',', 'HeaderLines', 1);
fclose(fid);

it looks like octave textscan is not fully compatible with matlab. 它看起来像octave textscan与matlab不完全​​兼容。 strread and textscan in Octave 3.4.0 are not fully compatible with their implementations in Matlab 2009b (and probably later versions as well). Octave 3.4.0中的strread和textscan与它们在Matlab 2009b(以及可能的更新版本)中的实现并不完全兼容。 For instance, the N=-1 option (repeat reading format until end of string) is not implemented in Octave 3.4.0 . 例如,在Octave 3.4.0中没有实现N = -1选项(直到字符串结尾的重复读取格式)。 Using a value of N=a positive integer (read format N times) does work the same as in Matlab. 使用N =正整数(读取格式N次)的值与Matlab中的相同。 I'll have to use fscanf but can it use delimiters 我将不得不使用fscanf,但它可以使用分隔符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM