Clear Filters
Clear Filters

How to manipulate nested cell arrays using for loop?

3 views (last 30 days)
Hi, I have a cell array of size 1x316 with each cell different sizes.In each cell, there is two columns of data: first is speed and second is time.Both are double numeric in types. Firstly, i loaded that cell array in a structure 's1'.Its variable is called ''all_cords''.Now, using for loop, i am trying to do some arithmatics here.i need to calculate pause time variable in a cell array of size 1x316.
I need to check in each cell, if there is 0's in first column i.e speed field,if there is single 0, then pause time will be its time value and if there are multiple 0's then pause time will be tn-t0.
Thanks in advance!
Here is my codes:-
s1 = load('cell_speed_data.mat');
tpause = cell(1,length(s1.all_cords));
pause_times = cell(1,length(s1.all_cords));
for i = 1:length(s1.all_cords)
if find(s1.all_cords{i}(:,1)==0)
tpause{i} = deal(s1.all_cords{i}(:,1));
pause_times{i} = max(tpause{i}(:,1))-min(tpause{i}(:,1));
end
continue;
end;
  2 Comments
Chad Greene
Chad Greene on 12 Mar 2016
This question is unclear. Can you provide some sample data and describe exactly what you are trying to do?
Guillaume
Guillaume on 16 Mar 2016
From a dimensional analysis point of view, there is some inconsistency in the result that you want.
If there is only one time where the speed is zero, you want as an output the time at which it occurs. Hence your output is a time.
If there is more than one time where the speed is zero, you want the difference between the first and last time, so your output is a duration.
Even if both have the same unit (time), they don't represent the same thing, so how is that output going to be useful?

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 16 Mar 2016
See my comment on your question about the mismatch in what the output represents. I'm creating two outputs here, pause_start which is the time at which the pause starts, and pause_duration which is the duration of the pause.
pause_start = cell(size(s1.all_coords)); %safer than length
pause_duration = cell(size(s1.all_coords)); %safer than length
for iter = 1:numel(s1.all_coords) %safer than length as well
ispause = s1.all_coord{iter}(:, 1) == 0; %no need for find, use logical array
pause_times = s1.all_coords{iter}(ispause, 2);
if ~isempty(pause_times)
pause_start{iter} = pause_times(1)
pause_duration{iter} = pause_times(end) - pause_times(1);
end
end
Note that the pause duration ignores cases where speed goes back up between start and end of the pause, as per your question. To me that doesn't sound correct but maybe it doesn't happen with your data, so it does not matter.
  5 Comments
Guillaume
Guillaume on 16 Mar 2016
Edited: Guillaume on 16 Mar 2016
Oh, of course, ispause is a column vector. Change the transitions line to:
transitions = find(diff([0; ispause; 0]));
I've edited my answer and fixed another bug later on. There may still be some more bugs. Code is untested.
Jung BC
Jung BC on 16 Mar 2016
Thanks a lot Guilaume,finally it's working well.
I really appreciate your help.One last request to you,
How can i extract minimum, maximum, and average
values from the final output- the cell array- pause_duration{}
and save them in different cell
arrays?

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!