删除空格并在txt中添加字符

时间:2010-10-02 13:02:55

标签: matlab file-io whitespace

我的MATLAB代码有问题。我想要的是你删除了一个空格中的所有空格。 Txt(下面的示例文件),并且可以在每行的末尾添加字符|||||||||||||

示例txt:

*|V|0|0|0|t|0|1|1|4|11|T4|H01|||||||
P|40|0.01|10|1|1|0|40|1|1|1||1|*||0|0|0||
*|A1|A1|A7|A16|F|F|F|F|F|F|F||||||||
*|cod.  serv|area |codice |nome|tnom|tmin|tmax|pc|qc|cond|susc||||||||
*|Ciao questa rete ha 32 nodi
*|||||kV|kV|kV|MW|MVAR|S|S||||||||
N|I|01|H01N01|H01N01|132|125.4|138.6|0|0||||||||||
N|I|01|H01N02|H01N02|20|19|21|0|0||||||||||
N|I|01|H01N03|H01N03|20|19|21|0.42318823|0.204959433||||||||||
N|I|01|H01N04|H01N04|20|19|21|0.087176748|0.042221634||||||||||
N|I|01|H01N05|H01N05|20|19|21|0.133064371|0.089419816||||||||||
N|I|01|H01N06|H01N06 |20|19|21|0.296231761|0.143471622||||||||||
N|I|01|H01N07|H01N07|20|19|21|0.07624009 |0.051233646||||||||||
N|I|01|H01N08|H01N08|20|19|21|0.078459073|0.037999473||||||||||
N|I|01|H01N09|H01N09|20|19|21|0.021794187|0.014645783||||||||||
N|I|01|H01N10|H01N10|20|19|21 |0.067710117|0.032793512||||||||||
N|I|01|H01N11|H01N11|20|19|21|0.080949906|0.054398667||||||||||
N|I|01|H01N12|H01N12 |20|19|21|0|0||||
N|I|01|H01N1B|H01N1B|20|19|21|0|0||||

MATLAB代码应删除空格字符agguingere ||||||。输出中的txt应该是这样的:

*|V|0|0|0|t|0|1|1|4|11|T4|H01|||||||||||||
P|40|0.01|10|1|1|0|40|1|1|1||1|*||0|0|0||||||||
*|A1|A1|A7|A16|F|F|F|F|F|F|F||||||||||||||
*|cod.serv|area|codice|nome|tnom|tmin|tmax|pc|qc|cond|susc||||||||||||||
*|Ciaoquestareteha32nodi||||||||||||
*|||||kV|kV|kV|MW|MVAR|S|S||||||||||||||
N|I|01|H01N01|H01N01|132|125.4|138.6|0|0||||||||||||||||
N|I|01|H01N02|H01N02|20|19|21|0|0||||||||||||||||
N|I|01|H01N03|H01N03|20|19|21|0.42318823|0.204959433||||||||||||||||
N|I|01|H01N04|H01N04|20|19|21|0.087176748|0.042221634||||||||||||||||
N|I|01|H01N05|H01N05|20|19|21|0.133064371|0.089419816||||||||||||||||
N|I|01|H01N06|H01N06|20|19|21|0.296231761|0.143471622||||||||||||||||
N|I|01|H01N07|H01N07|20|19|21|0.07624009|0.051233646||||||||||||||||
N|I|01|H01N08|H01N08|20|19|21|0.078459073|0.037999473||||||||||||||||
N|I|01|H01N09|H01N09|20|19|21|0.021794187|0.014645783||||||||||||||||
N|I|01|H01N10|H01N10|20|19|21|0.067710117|0.032793512||||||||||||||||
N|I|01|H01N11|H01N11|20|19|21|0.080949906|0.054398667||||||||||||||||
N|I|01|H01N12|H01N12|20|19|21|0|0||||||||||
N|I|01|H01N1B|H01N1B|20|19|21|0|0||||||||||

我认为这个问题的代码就是我不能错误地重写txt'输入单元格.....'

function [s]=correttore()
    clear all
    clc
    s = textread('Filein.txt','%s', 'delimiter', '\n', ...
        'whitespace', '');  % Read

    s=regexprep(s,'[\s'']',''); % Eliminate white space
    assignin('base','s',s) % Save workspace

    % Don't work

    fid=fopen(...)

    fprintf(fid,'Fileout.txt',s) % error 'cell input'

    % Don't work

    %For add a carapther ||||||||| i think

    str='|||||||||||||||||' % but don't work
    str=str2mat(str) % but don't work
    str=mat2cell(str(1,:)) % but don't work
    s(:,2)=str % but don't work

可能是cat错误?有什么建议吗?在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

未经测试:

function [s]=correttore()
 fidin = fopen('Filein.txt','r'); % read-only, no danger
 fidout = fopen('Fileout.txt','w');
 while ~feof(fidin)
  s = getl(fidin); % char array
  s = regexprep(s,'[\s'']',''); % Eliminate white space - not tested
  fprintf(fidout,'%s\n',[s,'|||||||||||||||||']);
end
fclose(fidin);
fclose(fidout);