Split table at n rows and plot data

조회 수: 8 (최근 30일)
Cody Brant Watson
Cody Brant Watson 2019년 6월 7일
댓글: dpb 2019년 6월 11일
I have a rather large .txt file (attached is an abbreviated version), there are about 500 cycles in the full file. Column 1(Var1) is the row count which is not always consistent, meaning sometimes it ends the cycle at the same row count, and other times it is different.
I used the text indicator on Var28, to split the table into 'Extend' and 'Retract' cells and then removed the Var28 string to make plotting easier. I need to plot each cycle Extend and Retract and find the min/max values and compare them with a specific value to see if they are in tolerance.
My question is, is there an easier way to do this rather than what I have started to do...? Matlab version: 9.2.0.556344 (R2017a)
% Test Run
clc
clear
%% Import Data
T = readtable('TestFileData.txt');
T(:,28) = [];
%% Split table using mat2cell
V = [8 6 9];
C = mat2cell(T,V,size(T,2));
% C{:,:} % to get all of the data
% C{1,1} Gives the first table (8x28 table)
E1 = C{1,1};
newE1 = table2array(E1); % convert table E1 to an array so we can plot
minVar17 = min(newE1(:,17));
maxVar17 = max(newE1(:,17));
% C{2,1} Gives the second table (6x28 table)
R1 = C{2,1};
% C{3,1} Gives the third table (9x28)
E2 = C{3,1};
%% Plot data
% Extend 1
x = newE1(:,2); % Column 2 i.e. time
yy1 = newE1(:,17); % Column 17
yy2 = newE1(:,18); % Column 18
plot(x,yy1,x,yy2)
title('Var17/18')
xlabel('Time(s)')
ylabel('Pressure(psig)')
grid on
legend(
'Var17','Var18','location','best')
I have added a PNG of the txt file I was using, right now I'm just using simulated data.
  댓글 수: 4
Cody Brant Watson
Cody Brant Watson 2019년 6월 8일
I'm not sure what the issue is...I can view and save the txt file (the text columns mentioned in the main post are in column Var28). The PNG also shows the orientation of the data for clarification.
dpb
dpb 2019년 6월 8일
The issue was I couldn't begin to read the image to make out what it was...and, I first couldn't find the text in the last column in the text file because of the line wrap issues...

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2019년 6월 8일
편집: dpb 2019년 6월 8일
In general, I'd process something like the following -- you can add control variables to select which columns you want plotted; I simply used the two you have in sample code as example; they can be any list desired. The first key is to determine where the sections of the file are...there are a couple of ways to do this; I chose to use the first column; one could also look at the last column--
t=readtable('TestFileData.txt'); % read the file into table
i1=find(t.Var1==1); % find beginning of each section
n=diff([i1;height(t)+1]); % compute number elements in each section
nG=numel(n); % how many groups there are...
ixPlt=[17 18]; % chosen columns to plot...
nP=numel(ixPlt); % number of same
stats=zeros(nG,nP); % preallocate for min,max of one column for groups
for i=1:nG % process each section in turn
i2=i1(i)+n(i)-1; % compute the end record number for this section
figure % start new figure
plot(t.Var2(i1(i):i2),t{i1(i):i2,[17 18]}) % plot the particular columns
% do whatever other calculations wish on this data section here before going on...
stats(i,:)=[min(t{i1(i):i2,[17 18]}) max(t{i1(i):i2,[17 18]})];
end
NB: Note the {} "curlies" to dereference the indexed addressing for the y plot array to return an array rather than table while the indexed variable .Var2 is an array.
The calculations and choice of columns is, of course, totally arbitrary mimicking your example code -- use user input or whatever else is appropriate to select those as well as what statistics or other calculations are done.
Another alternative as mentioned in initial comment would be to fill a grouping variable for each section as identified and one could then use the findgroups and splitapply sequence illustrated in documentation for those functions.
  댓글 수: 2
Cody Brant Watson
Cody Brant Watson 2019년 6월 11일
Thank you dpb for the help, and sorry for any confusion.
dpb
dpb 2019년 6월 11일
No problem...glad to help...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by