Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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

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
String matlab中文件的独立字符_String_Matlab_Chars - Fatal编程技术网

String matlab中文件的独立字符

String matlab中文件的独立字符,string,matlab,chars,String,Matlab,Chars,我在一个文件中有32个字符的字符串(多行)。 我想做的是创建一个新文件,并通过每个列4个字符的方式将它们放在那里 例如,我有: 00000000000FDAD000DFD00ASD00 00000000000FDAD000DFD00ASD00 00000000000FDAD000DFD00ASD00 .... 在新文件中,我希望它们显示如下: 0000 0000 000F DAD0 00DF D00A SD00 0000 0000 000F DAD0 00DF D00A SD00 有

我在一个文件中有32个字符的字符串(多行)。 我想做的是创建一个新文件,并通过每个列4个字符的方式将它们放在那里

例如,我有:

00000000000FDAD000DFD00ASD00

00000000000FDAD000DFD00ASD00

00000000000FDAD000DFD00ASD00

....
在新文件中,我希望它们显示如下:

0000 0000 000F DAD0 00DF D00A SD00

0000 0000 000F DAD0 00DF D00A SD00

有人能帮我吗?我现在工作了几个小时,找不到解决方案。

在Matlab中有一种方法:

% read in file
fid = fopen('data10.txt');
data = textscan(fid,'%s');
fclose(fid);

% save new file
s = size(data{1});
newFid = fopen('newFile.txt','wt');
for t = 1:s(1) % format and save each row
    line = data{1}{t};
    newLine = '';
    index = 1;
    for k = 1:7 % seven sets of 4 characters
        count = 0;
        while count < 4
            newLine(end + 1) = line(index);
            index = index + 1;
            count = count + 1;
        end
        newLine(end + 1) = ' ';
    end
    fprintf(newFid, '%s\n', newLine);
end
fclose(newFid);
%已读入文件
fid=fopen('data10.txt');
数据=文本扫描(fid,'%s');
fclose(fid);
%保存新文件
s=大小(数据{1});
newFid=fopen('newFile.txt','wt');
对于t=1:s(1)%s,格式化并保存每行
行=数据{1}{t};
换行符=“”;
指数=1;
对于k=1:7%,七组四个字符
计数=0;
当计数小于4时
换行符(结束+1)=行(索引);
指数=指数+1;
计数=计数+1;
结束
换行符(结束+1)='';
结束
fprintf(newFid,'%s\n',换行符);
结束
fclose(newFid);
导入

fid = fopen('test.txt');
txt = textscan(fid,'%s');
fclose(fid);
转换为一个M×28字符的数组,进行转置和重塑,使每列上有一个4字符的块。然后在底部添加一行空白并重新塑形。将每行存储在一个单元格中

txt = reshape(char(txt{:})',4,[]);
txt = cellstr(reshape([txt; repmat(' ',1,size(txt,2))],35,[])')
将每个单元格/行写入新文件

fid = fopen('test2.txt','w');
fprintf(fid,'%s\r\n',txt{:});
fclose(fid);

首先,打开输入文件并以字符串形式读取行:

infid = fopen(infilename, 'r');
C = textscan(infid, '%s', 'delimiter', '');
fclose(infid);
然后使用
regexprep
将字符串拆分为以空格分隔的4个字符组:

C = regexprep(C{:}, '(.{4})(?!$)', '$1 ');
最后,将修改后的行写入输出文件:

outfid = fopen(outfilename, 'w');
fprintf(outfid, '%s\n', C{:});
fclose(outfid);

请注意,此解决方案足够健壮,可以处理可变长度的行。

由于您已经处理此问题一段时间了,请向我们展示您到目前为止拥有的代码,并解释其不正确之处。是否使用换行符分隔每行?请注意,您的示例报告了28个字符,而不是32个字符。我特别喜欢
(?!$)
如果在末尾,则不匹配!