简体   繁体   English

八度高级textread用法,bash

[英]Octave advanced textread usage, bash

I have following text file: 我有以下文本文件:

079082084072079032084069067072000000000,0 082078032049050032067072065082071069000,1 076065066032065083083084000000000000000,0 082078032049050072082000000000000000000,1 082078032049050072082000000000000000000,1 082078032049050072082000000000000000000,1 070083087032073073032080068000000000000,0 080067065032049050032072082000000000000,0 082078032056072082000000000000000000000,1 070083087032073073073000000000000000000,0 082078032087069069075069078068000000000,1 082078032049050072082000000000000000000,1 077065073078084032077069067072032073073,0 082078032049050072082000000000000000000,1 080067065032049050032072082000000000000,0 082078032049050072082000000000000000000,1 079082084072079032084069067072000000000,0 082078032049050032067072065082071069000,1 076065066032065083083084000000000000000,0 082078032049050072082000000000000000000,1 082078032049050072082000000000000000000,1 082078032049050072082000000000000000000,1 070083087032073073032080068000000000000,0 080067065032049050032072082000000000000,0 082078032056072082000000000000000000000,1 070083087032073073073000000000000000000,0 082078032087069069075069078068000000000,1 082078032049050072082000000000000000000,1 077065073078084032077069067072032073073,0 082078032049050072082000000000000000000,1 080067065032049050032072082000000000000,0 082078032049050072082000000000000000000,1

I need too matrices: X size 16x13 Y size 16x1 我也需要矩阵:X大小16x13 Y大小16x1

I want to separate each row of the file into 13 values, example: 079 082 084 072 079 032 084 069 067 072 000 000 000 我想将文件的每一行分成13个值,例如:079 082 084 072 079 032 084 069 067 072 000000000

Is it possible to import it into octave using textread function? 是否可以使用textread函数将其导入八度?

If no, can it be done using Linux bash command? 如果否,可以使用Linux bash命令完成吗?

Yes, you can do this with textscan (see bottom if you really want to use textread : 是的,您可以使用textscan进行此textscan (如果您确实要使用textread请参见底部:

octave> txt = "079082084072079032084069067072000000000,0\n082078032049050032067072065082071069000,1";
octave> textscan (txt, repmat ("%3d", 1, 13))
ans = 
{
  [1,1] =

    79
    82

  [1,2] =

    82
    78

  [1,3] =

    84
    32

  [1,4] =

    72
    49
[...]

Note that you are reading them as numeric values, so you do not get the preceding zeros. 请注意,您正在将它们读取为数值,因此不会获得前面的零。 If you want them, you can either read them as string by using "%3s" in the format (extra trouble to handle and reduced performance since you will then be handling cell arrays). 如果需要它们,可以通过使用格式为“%3s”的字符串来读取它们(处理麻烦,并且降低了性能,因为随后将要处理单元阵列)。

Since you are reading from a file: 由于您正在读取文件:

[fid, msg] = fopen ("data.txt", "r");
if (fid)
  error ("failed to fopen 'data.txt': %s", msg);
endif 
data = textscan (fid, repmat ("%3d", 1, 13));
fclose (fid);

If you really want to use textread : 如果您真的想使用textread

octave> [d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13] = textread ("data.txt", repmat ("%3d", 1, 13))
d1 =

  79
  82
  76
[...]    
d2 =

  82
  78
  65
[...]

or: 要么:

octave> data = cell (1, 13);
octave> [data{:}] = textread ("data.txt", repmat ("%3d", 1, 13))
data = 
{
  [1,1] =

    79
    82
    76
[...]

  [1,2] =

    82
    78
    65
[...]

If you need to capture the value after the comma (not really part of your original question), you can use: 如果您需要在逗号后捕获值(这并不是您原始问题的一部分),则可以使用:

octave> textscan (txt, [repmat("%3d", 1, 13) ",%1d"])
ans = 
{
  [1,1] =

    79
    82

  [1,2] =

    82
    78

  [1,3] =

    84
    32

[...]

  [1,14] =

0
1

} }

You can do this pretty easily by reading three characters at a time using read in the shell: 您可以使用shell中的read一次读取三个字符来轻松完成此操作:

while IFS="${IFS}," read -rn3 val tail; do
    [[ $tail ]] && echo || printf '%s ' "$val"
done < file

This implementation assumes that if we encounter a value after the comma, we should go to the next line. 此实现假定如果在逗号遇到一个值,则应转到下一行。

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

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