【问题标题】:remove whitespace and add character in txt删除空格并在txt中添加字符
【发布时间】:2010-10-02 13:02:55
【问题描述】:

我对 MATLAB 代码有疑问。我想要的是您删除 a 中的所有空白。 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 'input cell .....'

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 错误?有什么建议吗?在此先感谢您的帮助。

【问题讨论】:

    标签: matlab file-io whitespace


    【解决方案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);
    

    【讨论】:

    • 是的,效果很好!非常感谢你。解析文本非常快。我不知道函数'fgetl',它很有用。我还有一个小问题。
    猜你喜欢
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多