Parsing Text File with Matlab

5 visualizzazioni (ultimi 30 giorni)
Lucas S
Lucas S il 28 Mar 2019
Commentato: Lucas S il 29 Mar 2019
Hello !
I'm new with matlab and i'm trying to parse a text file with Matlab which contains my own markup language.
[Date]
2019-03-27 10:45:10.167618
[Component]
Component_Name : Manager principal
Component_ID : _ocl_MEu9Eemg_bhrv2HEbw
{Port}
Port_Name : FOP 1
Port_ID : _sZMXoEu9Eemg_bhrv2HEbw
Port_Type : Outgoing Port
[Component]
Component_Name : Manager 2
Component_ID : _r-HlMEu9Eemg_bhrv2HEbw
{Port}
Port_Name : FIP 1
Port_ID : _sZWIoku9Eemg_bhrv2HEbw
Port_Type : Incoming Port
[Link]
Link_Name : On/Off
Link_Source_Name : Manager principal
Link_Source_ID : _ocl_MEu9Eemg_bhrv2HEbw
Link_Source_Port_Name : FOP 1
Link_Source_Port_ID : _sZMXoEu9Eemg_bhrv2HEbw
Link_Target_Name : Manager 2
Link_Target_ID : _r-HlMEu9Eemg_bhrv2HEbw
Link_Target_Port_Name : FIP 1
Link_Target_Port_ID : _sZWIoku9Eemg_bhrv2HEbw
I already make this code :
function testLectureXML()
componentBalise = '[Component]';
componentNameBalise = 'Component_Name : ';
componentIdBalise = 'Component_ID : ';
PortBalise = '{Port}';
PortNameBalise = 'Port_Name : ';
PortIdBalise = 'Port_ID : ';
PortTypeBalise = 'Port_Type : ';
linkBalise = '[Link]';
linkNameBalise = 'Link_Name : ';
str_buf = fileread( 'capella_to_matlab.txt' );
component_list = strfind(str_buf, componentBalise);
link_list = strfind(str_buf, linkBalise);
n_link = numel(link_list);
n_component = numel( component_list );
for ii = 1 : n_component
ix1 = component_list(ii)+ length(componentBalise) + 2; %+2 pour retirer le retour chariot
if ii == n_component
buf = str_buf( ix1 : end );
else
buf = str_buf( ix1 : component_list(ii+1)-1 ); % Contenus de chaque balise [Composant]
end
component_NameStart = strfind(buf, componentNameBalise) + length(componentNameBalise); % Chaque élément dans le fichier txt de type "Component_name : "
component_IDStart = strfind(buf, componentIdBalise) + length(componentIdBalise);
carrierReturn = strfind(buf, char(13)); % Postion de chaque retour à la ligne
component_NameValue = buf(component_NameStart : carrierReturn - 1); % On récupère la valeur des "Component_Name : "
component_IDValue = buf(component_IDStart : carrierReturn - 1);
while contains(component_NameValue, ' ')
component_NameValue = strrep(component_NameValue, ' ', '_'); % On ajoute un _ à la place des espaces dans les noms des composants
end
new_system(component_NameValue);
And i don't understand why i success to get component_NameValue which is Manager principal but not its ID with component_IDValue which returns me "empty 1x0 char"
Thanks for help !

Risposte (1)

Jan
Jan il 29 Mar 2019
Are you sure you want to use char(13) as linebreak? While the DOS line breaks char([10,13]) are used e.g. by the old version of Microsoft's NotePad, all other modern editors are happy with char(10). I assume this might be the problem here.
This loop is meaningless
while contains(component_NameValue, ' ')
component_NameValue = strrep(component_NameValue, ' ', '_'); % On ajoute un _ à la place des espaces dans les noms des composants
end
Simply omit the loop, because the first strrep command removes all spaces.
  1 Commento
Lucas S
Lucas S il 29 Mar 2019
Thanks for your answer ! For the loop i just delete the loop and indeed it's working anyway and for the char(13) and char(10) i found my problem. I have understood after i post this question that carrierReturn is an array with the position of each carrier Return so i just used :
component_IDValue = buf(component_IDStart : carrierReturn(2);

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by