How can I divide my text file in several blocks?

2 views (last 30 days)
I want to read my text file "January_5d" and divide it in 5 different blocks(day by day)because I would like to plot the several trending. What I have so far is:
% code
YourFilePath = 'C:\Users\Matlab\January_5d.txt';
fid = fopen(YourFilePath ,'rt');
Datafile = textscan(fid,'%s%s%s%s%s','Headerlines',2);
%Allocate all the measured value in the "Value1" variable
Value = Datafile{3};
Value = cellfun(@(x) textscan(x,'%s','Delimiter', ' ')',Value ,'UniformOutput',false);
Y = vertcat(Value{:});
X = cellfun(@transpose,Y,'UniformOutput',false);
Z = cellfun(@str2double,X,'UniformOutput',false);
Value1=cell2mat(Z);
%Allocate all the times in the "Time1" variable
Time (:,1)= Datafile{2};
Time1=cell2mat(Time);
%close the text file
fclose(fid);
With this code I can read all the file and store all the data in the 2 variables 'Value1' and 'Time1' but I don't know how I could store the data in 5 different blocks(day by day).Can someone help me?

Accepted Answer

per isakson
per isakson on 13 May 2014
Edited: per isakson on 13 May 2014
This function outputs a diagram for day one
function cssm
YourFilePath = 'h:\m\cssm\January_5d.txt';
fid = fopen( YourFilePath, 'rt' );
Datafile = textscan( fid, '%s%s%f%s%s' ...
, 'Headerlines',2, 'CollectOutput', true );
fclose(fid);
%Allocate all the measured value in the "Value1" variable
Value1 = Datafile{2};
%Allocate all the times in the "Time1" variable
Time1 = datevec( cell2mat( Datafile{1} ), 'yyyy-mm-ddHH:MM:SS' );
block1 = Value1( Time1(:,3)==1 );
t1 = Time1 ( Time1(:,3)==1, : );
figure, plot( datenum(t1)-datenum(t1(1)), block1 )
end
  3 Comments
per isakson
per isakson on 13 May 2014
Edited: per isakson on 13 May 2014
On CollectOutput doc says
If true, then textscan concatenates consecutive output cells of
the same fundamental MATLAB class into a single array.
I think that data and time concatenated makes the code easier to read and understand. Yes, this just one way out of several alternatives.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export 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!