How can I divide my text file in several blocks?

1 visualización (últimos 30 días)
Matteo
Matteo el 13 de Mayo de 2014
Comentada: Matteo el 13 de Mayo de 2014
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?

Respuesta aceptada

per isakson
per isakson el 13 de Mayo de 2014
Editada: per isakson el 13 de Mayo de 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 comentarios
per isakson
per isakson el 13 de Mayo de 2014
Editada: per isakson el 13 de Mayo de 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.
Matteo
Matteo el 13 de Mayo de 2014
You are right!thank you very much!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by