views:

51

answers:

2

Hello. 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 advance for the help.

+1  A: 

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);
Alex
yes work very very well! Thank you very much. is very quick to parse text. I did not know the function 'fgetl', it is useful. I have another little question.
Marietto85
A: 

I have another question, in the txt. This text file is output to an old program done in C or Fortlan. sometimes the comments marked with * | not appear and there are only white lines. The file example should exemplify:

*|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|||||||||||||||||||||||
*|||||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|||||||||||||||||||||
|||||||||||||||||
|||||||||||||||||
L|I|H010203|H01N02|H01N03|1.884|360|0.41071|0.207886957||3.19E-08|3.19E-08|||||||||||||
L|I|H010304|H01N03|H01N04|1.62|360|0.35316|0.1787563||3.19E-08||3.19E-08||||||||||||
L|I|H010405|H01N04|H01N05|0.532|360|0.11598|0.058702686||3.19E-08||3.19E-08|||||||||||
L|I|H010506|H01N05|H01N06|1.284|360|0.27991|0.14168092||3.19E-08||3.19E-08||||||||||||
S|SH01|SEZIONE01|1|-3|+3|-100|+100|||||||||||||||||||
S|SH02|SEZIONE02|1|-3|+3|-100|+100|||||||||||||||||||
S|SH03|SEZIONE03|1|-3|+3|-100|+100|||||||||||||||||||
||||||||||||asasasas
S|SH04|SEZIONE04|1|-3|+3|-100|+100|||||||||||||||||||
*|comment
S|SH05|SEZIONE05|1|-3|+3|-100|+100|||||||||||||||||||

should become:

*|V|0|0|0|t|0|1|1|4|11|T4|H01||||||||||||||||||||||
*|comment
*|comment
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|||||||||||||||||||||||
*|||||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|||||||||||||||||||||
*|comment||||||||||||||||
*|comment|||||||||||||||||
L|I|H010203|H01N02|H01N03|1.884|360|0.41071|0.207886957||3.19E-08||3.19E-08||||||||||||
L|I|H010304|H01N03|H01N04|1.62|360|0.35316|0.1787563||3.19E-08||3.19E-08|||||||||||||||
L|I|H010405|H01N04|H01N05|0.532|360|0.11598|0.058702686||3.19E-08||3.19E-08||||||||||||
L|I|H010506|H01N05|H01N06|1.284|360|0.27991|0.14168092||3.19E-08||3.19E-08||||||||||||
*|comment
*|comment
S|SH01|SEZIONE01|1|-3|+3|-100|+100|||||||||||||||||||
S|SH02|SEZIONE02|1|-3|+3|-100|+100|||||||||||||||||||
S|SH03|SEZIONE03|1|-3|+3|-100|+100|||||||||||||||||||
S|SH04|SEZIONE04|1|-3|+3|-100|+100|||||||||||||||||||
S|SH05|SEZIONE05|1|-3|+3|-100|+100|||||||||||||||||||

The data are divided into 'packages' distinct from the first letter (PNLS). Each package must have at least two dedicated lines (* |) which is then read as a comment. The white lines between different letters are filled with character * |. The lines between various letters that do not begin with * | to be added. The white lines and characters 'random' between identical letters are removed.

Perhaps it is more clear in the example files. It is very hard to implement the logic that should do what, I'm bad in parse data.

Marietto85
@Marietto85, please don't add updates or new questions as an answer to your question. For new question you obviously better to create new question. If it's new information update your question with 'edit' button. For followup or clarify someone's answer make a comment to it.
yuk
You have reason. sorry.
Marietto85

related questions