How can I divide my text file in several blocks?
조회 수: 1 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
per isakson
2014년 5월 13일
편집: per isakson
2014년 5월 13일
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
per isakson
2014년 5월 13일
편집: per isakson
2014년 5월 13일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!