How to create plot from the data in .txt file ?

Hello All,
I wanted to create a 2D plot of spatial density versus altitude using the data in .txt file, however, when i tried converting first the data into excel format it did not work the best and eventually i could not do the necessary coding to read data from excel file to create the desired MATLAB plot as shown below.
Kindly could anyone let me know, if there is way you could suggest to directly create a 2D plot using the attached .txt file or at least convert them to excel format that can be coded in MATLAB to create the plot?

 채택된 답변

Les Beckham
Les Beckham 2023년 8월 31일
This should get you started. Note that the warnings are expected and can be ignored.
T = readtable('Spatial Density.txt', 'EmptyLineRule', 'skip', 'VariableNamesLine', 1);
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
% plot some of the data -- modify as desired
plot(T.Altitude, T.Expl_Fragm, T.Altitude, T.Coll_Fragm, T.Altitude, T.Launch_Mis)
grid on
legend(T.Properties.VariableNames, 'Interpreter', 'none')
Warning: Ignoring extra legend entries.

댓글 수: 7

Rufiya
Rufiya 2023년 8월 31일
Hello Les,
I tried the above code, but unfortunately continued to recieve error messages and did not create the above plot. However, I managed to create the plot i requested by coverting the info from .txt into excel sheet.
In case if you happened to have any other alternate methods, please do share.
I'm glad you were able to generate the plot that you needed.
You shouldn't have needed to create a spreadsheet. readtable can read the text file directly, as I did above. I can't help you understand an error message that I can't see, caused by code that I also can't see.
Should be
legend({'Expl_Fragm', 'Coll_Fragm', 'Launch_Mis'}, 'Interpreter', 'none')
Rufiya
Rufiya 2023년 8월 31일
Hello Les,
I received the below error messages:
Error using readtable
Unable to find or open 'Spatial Density.txt'. Check the path and filename or file permissions.
Error in Test (line 1)
T = readtable('Spatial Density.txt', 'EmptyLineRule', 'skip', 'VariableNamesLine', 1);
The error message is pretty clear. Matlab can't find the file.
Is the text file in the same folder with your script that you are using to make the plot?
Rufiya
Rufiya 2023년 9월 3일
Dear Les,
Sorry for the mistake!
Since i earlier changed the name of text document, the above code did not unfortunately recognize the txt file mentioned above in the code and hence caused an error.
The above code which you had given me is correct and generates the desired results.
Many Thanks!
You are quite welcome.

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

추가 답변 (1개)

Zak
Zak 2023년 8월 31일
Would something like this work? You'll have to modify the plotting part to add the other columns. I'm sure there's some way to name the table variables from the text file, but I didn't quite figure it out yet.
T = readtable('Spatial Density.txt','NumHeaderLines',1);
figure
hold on
plot(T.Var1,T.Var2)
plot(T.Var1,T.Var3)
hold off
xlabel('altitude')
legend('Expl-Fragm','Coll-Fragm')

댓글 수: 4

Rufiya
Rufiya 2023년 8월 31일
Hello Zak,
Thanks for sharing the code.
I tried the above code, but unfortunately continued to recieve error messages and did not create the above plot. However, I managed to create the plot i requested by coverting the info from .txt into excel sheet.
In case if you happened to have any other alternate methods, please do share.
Zak
Zak 2023년 8월 31일
What kind of error messages did you get?
Rufiya
Rufiya 2023년 8월 31일
Hello Zak,
I received the below error messages:
Error using readtable
Unable to find or open 'Spatial Density.txt'. Check the path and filename or file permissions.
Error in Test (line 1)
T = readtable('Spatial Density.txt','NumHeaderLines',1);
Please show us the output from these MATLAB commands:
pwd
ls Spat*
ls *.txt

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

카테고리

태그

질문:

2023년 8월 31일

댓글:

2023년 9월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by