How to plot a .txt file when the data is in blocks and each data is separated by space

11 views (last 30 days)
Hi All,
In the .txt file I have the first block as x axis, the data is not in column i.e. the first value is 1st row (1.0000E-08), second value (1.5850E-08) is second row and last value is last row (1.0000E+01 )
1.0000E-08 1.5850E-08 2.5120E-08 3.9810E-08 6.3100E-08 1.0000E-07 1.5850E-07
2.5120E-07 3.9810E-07 6.3100E-07 1.0000E-06 1.5850E-06 2.5120E-06 3.9810E-06
6.3100E-06 1.0000E-05 1.5850E-05 2.5120E-05 3.9810E-05 6.3100E-05 1.0000E-04
1.5850E-04 2.5120E-04 3.9810E-04 6.3100E-04 1.0000E-03 1.5850E-03 2.5120E-03
3.9810E-03 6.3100E-03 1.0000E-02 1.5850E-02 2.5120E-02 3.9810E-02 6.3100E-02
1.0000E-01 1.5850E-01 2.5120E-01 3.9810E-01 6.3100E-01 1.0000E+00 1.5850E+00
2.5120E+00 3.9810E+00 6.3100E+00 1.0000E+01
Now the second block is my y axis, and the data is arranged silimarly as above.
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 5.7314E+00 0.0000E+00
The file has multiple plot data in it. Therefore, I am trying to first arrange the data in separate coloums and use some coloums for plotting as Y axis value block is repeated twice for each case.
It will be great if someone please give me some direction to start on this.
Thank you in advance for your time and attention.

Accepted Answer

Chris
Chris on 23 Apr 2022
Edited: Chris on 25 Apr 2022
Wow, that is an inconvenient format.
One option is to use the data import tool--right-click on the text file in matlab and "import data", choose "space" as the delimiter, and highlight everything before importing. Then you can pick your data out of the matrix as needed.
Another option, if you're going to be doing this often, is to import the data as strings using readlines().
data = readlines('Source1_.txt');
Then, in a for loop, split each line using something like
for idx = 1:numel(data)
temp = readlines(idx).strip.split;
...
end
This will give you strings you can analyze. On the first line, for instance,
str2double(temp(1))
tells you the number of data points for the first block.
I can't see how to get the number of blocks in a section, though, so you may have to then look for a line that indicates the end of the section.
Or, just copy/paste things from Excel.
Edit:
The following is incredibly fragile, but it will give you a cell array of the data for this specific file.
data = readlines('Source1_.txt');
dataswitch = false;
alldata = [];
datavect = [];
for idx = 1:numel(data)
temp = char(data(idx).strip.split);
if all(size(temp) == [4,2]) || all(size(temp) == [4,3])
% Get the size of a block
numpts = str2double(temp(1,:));
elseif size(temp,2) >= 10 && any(temp(1,[7,8])=='E')
% we see scientific notation
switch dataswitch
case false
dataswitch = true;
datavect = double(string(temp));
case true
datavect = [datavect; double(string(temp))];
end
if numel(datavect) == numpts % End of block
alldata = [alldata,{datavect}];
datavect = [];
dataswitch = false;
end
% On review, the 'else' appears to be redundant
% else % Not data
% switch dataswitch
% case true
% % We just passed the end of a block
% dataswitch = false;
% alldata = [alldata,{datavect}];
% datavect = [];
% case false
% % next iteration
% end
end
end
Access the first block like alldata{1};
If you want to plot the second and third blocks against the first:
figure
plot(alldata{1},[alldata{2:3}])
or
figure
hold on
plot(alldata{1},alldata{2})
plot(alldata{1},alldata{3})
  5 Comments
Ron
Ron on 25 Apr 2022
Hi Chris,
Thank you very much.
Yes, the issue was on running the program on older version. Running it on the newer version worked fine.
Thank you very for the code.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Model Editing in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!