1

I need to read data from a file and plot a graph with its data. The problem is:

  • (1) I can't change the format of data in the file
  • (2) The format contains information and characters that I don't know how to deal with.

Here is a part of the data file, it's in a txt format:

Estation;Date;Time;Temp1;Temp2;Pressure;
83743;01/01/2016;0000;31.9;25.3;1005.1;
83743;01/01/2016;1200;31.3;26.7;1005.7;
83743;01/01/2016;1800;33.1;25.4;1004.3;
83743;02/01/2016;0000;26.1;24.2;1008.6;

What I'm trying to do is to plot the Date and Time against Temp1 and Temp2, not worrying about Pressure. The first column can be neglected as well. How can I extract the Date, Time and Temps into and matrix so I can plot them? All I did so far was this:

fileID = fopen('teste.txt','r');
[A] = fscanf(fileID, ['%d' ';']);
fclose(fileID);
disp(A);

Which just reads the first value, 83743.

2
  • Do you mean a plot i.e. graphical representation of your data, or do you simply want to display it, like you do in your code? They are vastly different. Commented Feb 25, 2017 at 13:05
  • I mean plot, graphical representation. But I believe that to do so I'll have to extract the data into a matrix, right? Commented Feb 25, 2017 at 16:06

2 Answers 2

2

To build on m7913d's answer:

fileID = fopen('MyFile.txt','r');
A = fscanf(fileID, ['%s' ';']); % read the header line
B = fscanf(fileID, '%d;%d/%d/%d;%d;%f;%f;%f;', [8,inf]); % read all the data into B (the date is parsed into three columns)
fclose(fileID);
B = B.'; % transpose B
% C is just for verification, can be omitted
C = datetime([B(:,4:-1:2) B(:,5)/100zeros(numel(B(:,1)),2)],'InputFormat','yyyy-MM-dd HH:mm:ss'); 
D = datenum(C);   % Get the date in a MATLAB usable format

Titles = strsplit(A,';'); % Get column names

figure;
hold on % hold the figure for multiple plots
plot(D,B(:,6),'r')
plot(D,B(:,7),'b')
datetick('x') % Set a date tick as axis
legend(Titles{4},Titles{5}); % uses titles for legend

note the parsing of the date into C: first is the date as given by you in dd-MM-yyyy format, which I flip to the official standard of yyyy-MM-dd, then your hour, which needs to be divided by 100, then a 0 for both minutes and seconds. You might need to rip those apart when you don't have exactly hourly data. Finally transform to a regular datenum, which MATLAB can use for processing.

Which results in:

enter image description here

You might want to play around with the datetick format, as it's got lots of options which might appeal to you.

Sign up to request clarification or add additional context in comments.

Comments

1
fileID = fopen('input.txt','r');
[A] = fscanf(fileID, ['%s' ';']); % read the header line
[B] = fscanf(fileID, '%d;%d/%d/%d;%d;%f;%f;%f;', [8,inf]); % read all the data into B (the date is parsed into three columns)
fclose(fileID);
disp(B');

Note that %d reads an integer (not a double) and %f reads a floating point number.

See fscanf for more details.

4 Comments

There's no plot here, which is what he wants.
We are getting closer. I tested your code...it works, but it only reads the first 2 lines and the numbers are little messed up, like, the date was supposed to be and integer, but it shows 0.0001, for example. And the float points, like temp is showing 0.0032 instead of 31.9. Any idea why is this happening?
There is no plot, but the data is extracted into a matrix, wich I can plot later.
@Alexandre not true, you're missing the 1.0e+04 * above your matrix. These are the right numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.