how to Read Txt file(XML format) in Matlab ?

조회 수: 5 (최근 30일)
Shiv Nileshkumar Matliwala
Shiv Nileshkumar Matliwala 2022년 11월 28일
댓글: Shiv Nileshkumar Matliwala 2022년 11월 29일
Lets assume I have text file name 'Text.txt' and I have below data in it,
<Name>
New_Model
</Name>
<Model>
Model1.slx
Model2.slx
Model3.slx
</Model>
Now, inside matlab, I want to open this file with read mode.
And I want to extract the name 'New_Model' and 'Model1.slx', 'Model2.slx' and 'Model3.slx'
how can extract these name using a more general way?
Like between <Name> and </Name> there is a file name. I should be able to extract the name from this
and between <Models> and </Models> there are 3 model names. So, I should be able to extract the names of these models.
I have tried the specific way as below,
fileID = fopen('Text.txt','r');
model_s = fscanf(fileID,'<Name> %s </Name>')
model1 = fscanf(fileID,'%*s\n %s',1)
model2 = fscanf(fileID,'%s\n %*s',1)
model3 = fscanf(fileID,'%s\n %*s',1)
fclose(fileID)
Now, looking for a general way to do it. Like I have dine it above in the case of <Name>.
  댓글 수: 2
David Hill
David Hill 2022년 11월 28일
Please attach your Text.txt file.
Shiv Nileshkumar Matliwala
Shiv Nileshkumar Matliwala 2022년 11월 29일
Hello David,
I have already uploaded the content in the question. The file name is Text.txt and it has the above content in it. It is just a simple text file with content as below,
<Name>
New_Model
</Name>
<Model>
Model1.slx
Model2.slx
Model3.slx
</Model>
Now here just I want to extract New_Model and store it is variable 'name' and Model1.slx, Model2.slx and Model3.slx and store it in variable 'models' for example. but only by using this <Name> and </Name>, So whatever is there between this two lines, It is my 'name' and whatever is there between <Model> and </Model> lines, those are my three different model names 'models'.

댓글을 달려면 로그인하십시오.

답변 (1개)

Fangjun Jiang
Fangjun Jiang 2022년 11월 28일

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by