开发者

remove whitespace and add character in txt

开发者 https://www.devze.com 2023-01-18 06:25 出处:网络
I have a problem with MATLAB code. What I want is that you delete all the blanks in a. Txt (example file below) and that one could add character ||||||||||||| to the end of each line.

I have a problem with MATLAB code. What I want is that you delete all the blanks in a. Txt (example file below) and that one could add character ||||||||||||| to the end of each line.

Example 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||||

The MATLAB code should remove whitespace characters agguingere ||||||. txt in the output should be like this:

*|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||||||||||

The code that I thought is this the problem is that I can not rewrite the txt by mistake '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

Maybe is a cat error? Any suggestion? Thanks in adv开发者_开发百科ance for the help.


not tested:

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消