Plot data from text file
Show older comments
I want to plot ReP^2+ImP^2 as function of numero = 5, 10, 15 …145 for each mode(m,n) I would like to have something like this with the name of the mode on each curve. Here is the beginning of my code, anyone can help ?
data = readfile(test.txt)
HeaderLines = 8;
for i=(HeaderLines+1):numel(tm)
%store in temp variable
if isempty(data{i}),continue,end
str=data{i};
line = str2num(str);
....
You can find my file attached.

Thank you in advance,
5 Comments
Rik
on 23 Sep 2020
Without knowing how your data is stored in your text file it is difficult to give you advice. Is it the same structure as you have described here?
It is a good idea to read all your data into Matlab variables and only then do the plotting. That way you separate the two tasks. If you want to recycle either the plotting or the reading for a later project you will not have to go through every line to determine what you need. That modularity is a very useful strategy you see in action with my readfile function: instead of writing the same code again, I use the function.
Izem
on 23 Sep 2020
Rik
on 23 Sep 2020
That answered that question, yes.
The way I see it, you need to follow these steps:
- Load the data into Matlab variables
- Get the data for a single line
- Plot the line and create an annotation (e.g. with the text function)
- Repeat 2-3 for each combination of n and m
Try each step. If you have trouble with any of them, please describe what you tried.
Izem
on 23 Sep 2020
Rik
on 23 Sep 2020
You should convert the entire file. That means you will have a vector m with 5145 elements. Then you can use unique and use the result for a for loop, in which you can use ismember to extract all positions where m has a specific value.
You can also use that line to extract the values of m and n you need to combine.
Answers (0)
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!