MATLAB Answers

Split table at n rows and plot data

18 ビュー (過去 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: (R2017a)
% Test Run
%% 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
grid on
I have added a PNG of the txt file I was using, right now I'm just using simulated data.

  4 件のコメント

表示 1 件の古いコメント
dpb 2019 年 6 月 7 日
That's totally illegible; attach the beginnings of the text file itself...we can't try to solve your problem unless we have some way to generate an input that at least mimics the problem trying to be solved... :)
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 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 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]})];
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 2019 年 6 月 11 日
No problem...glad to help...


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by