Delete specific rows from a cell array and create a new cell array

Hello Everyone,
I have created a 1x8 cell array from a text file. I want to create a new cell array that will only have a particular feature. If my cell array is "S" then I want to use a condition on S{8}. if any element (n) of S{8} is greater than its next element (n+1) I want to delete the entire row and after deleting all the unwanted rows, I want to create a new cell array with the remaining elements.
For example:
if I have these elements in my cell array "S"
0,0,1,0,0,0,0,10761
0,0,1,0,0,0,0,11067
0,0,1,0,0,0,0,3036687367
0,0,1,0,0,0,0,11271
.
.
0,0,0,0,0,0,0,28509
0,0,0,0,0,0,0,1656057693
0,0,0,0,0,0,0,28713
0,0,0,0,0,0,0,28815
I want to create a new cell array "T" that will only have these elements:
0,0,1,0,0,0,0,10761
0,0,1,0,0,0,0,11067
0,0,1,0,0,0,0,11271
.
.
0,0,0,0,0,0,0,28509
0,0,0,0,0,0,0,28713
0,0,0,0,0,0,0,28815
Any Suggestions?
Thanks.

Answers (1)

Place where converting the cell array to regular double array makes sense it would seem...there's nothing in the above data that isn't numeric so just use the native double array.
S=cell2mat(S); % convert to double array
ix=[false; (diff(S(:,8))>0)]; % locations of +-ive difference
S(ix,:)=[]; % remove those rows

5 Comments

Thank you very much for your kind reply. So for this is what I have done:
fid=fopen('OVRNITV1.txt','r');
if fid == -1
disp('Error,oprning txt file')
else
S=textscan(fid, '%d %d %d %d %d %d %d %d', 'Delimiter', ',');
T=cell2mat(S); % convert to double array
ix=[false; (diff(T(:,8))>0)]; % locations of +-ive difference
T(ix,:)=[]; % remove those rows
end
fclose(fid);
Result:
Here you can see that the code has also skipped some data points (in S{1,8} Row :42 is now Row: 3 in T matrix) and many more.
But I want to keep those values as well. Is there a way to solve this problem?
I have checked all the data sets and so far this is the condition I have found that will work the best:
n-1<n<n+1
if any element of S{8}, does not satisfy this condition then that whole row should be deleted. This might help writing the algorithm.
And I want to add another thing. Is it possible to convert the 'T' matrix into a {1,8} cell again? I am asking because later on, I want to plot each of the 1 to 7 (S{1} to S{7}) columns against column 8 (S{8}) using something like this command:
x=plot(S{8},S{1}, '-');
xlim([561 77973748]);
ylim([-1 2]);
Can I use this command for this purpose?
C = num2cell(T,[1 8]);
Again, thanks for the advice.
" ... later on, I want to plot each of the 1 to 7 (S{1} to S{7}) columns against column 8 (S{8}) using something like this command:"
Just use
plot(S(:,1),S(:,7))
instead once have the double array. One colon and a comma are no more characters than two curlies. :)
fid=fopen('OVRNITV1.txt','r');
if fid == -1
error('Error opening txt file')
end
T=cell2mat(textscan(fid, '%d %d %d %d %d %d %d %d', 'Delimiter', ','));
fid=fclose(fid);
ix=[false; (diff(T(:,8))>0)]; % locations of +-ive difference
T(ix,:)=[]; % remove those rows
removing superfluous code...convert to array when read; once you have the array there's no need for the cell array any longer.
"Here you can see that the code has also skipped some data points (in S{1,8} Row :42 is now Row: 3 in T matrix) and many more."
No, we can't see anything of the sort. What does "the code has skipped some data points" mean? It deleted everything for which diff(S)>0 as per the original specification--that there are duplicated values and therefore a set of points for which the difference is now zero isn't anything that was given as a requirement.
Would have to see all the original data to know; perhaps the condition should be
ix=[false; (diff(T(:,8))>=0)];
but can't tell without a definitive statement of what is the desired result; some heuristic that may work for a given dataset isn't a specification.
@ dpb, Thank you very much for your advice, after the holidays I tried to followup the code and used a different approach, in a way the same thing that you did but with a different approach:
for i=2:size(T,1)
if ~(T(i-1,8)<T(i,8) && T(i,8)<T(i+1,8))
fprintf('%s%i%s%i%s','Problem in line: ', i,' Value is: ',T(i,8),newline);
T(i,:) = [];
end
if i> size(T,1)-1
break;
end
end
That portion worked just fine. but now I am stuck with a different problem. Even after deleting those points I am left with few more points that do not allow me to plot everything property. Just to recap, column 8 is the value of "time" in milliseconds (x-axis) and all the other columns are the corresponding values of 7 different sensors (y-axis). Anyway, after using the previously mentioned code, I only tried to plot the column 8. with this command:
figure(3);
% x2=plot(C1{8}, '*');
plot(T(:,8), '*')
ylim([-1 80000000]);
This was the result:
Untitled.png
So, the next challenge is that I want to keep all the data points that will create that solid blue (red arrow) line and want to delete all the data points from the matrix that is above or below that line. My instinct tells me that I should figure out the equation (y=mx) of that line and use that equation as the condition of the "if loop" that I mentioned earlier, to remove those unwanted data points.
I can manually select two points from the graph and figure out the equation of that line. But is there a function in MATLAB that will automatically detect the equation of that line?
and
Is there any command in MATLAB that will automatically delete all the data points above or below that line?
Again thank you very much for all the help.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 13 Dec 2019

Commented:

on 28 Dec 2019

Community Treasure Hunt

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

Start Hunting!