You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
So I wanted to split the 1X15 cell to different cells.
2 views (last 30 days)
Show older comments
![5.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238774/5.jpeg)
Yes this is a screenshot of a screenshot.
So I have a 1x15 cell
Each (cell)column is one channel so a total of 15 channels.
2nd screenshot is a 1st channel with 2 states.
So now i wanted to put each state of one channel into a seperate matrix or a cell(preferably a cell).The state changes from rest to active every 200th iteration and vice versa.
So new cell of channel 1 would contain 1 cell for with values for rest block and next cell for active block and so on.
so repeat this for 15 channels.
I have attached mat file of my data.
3 Comments
Guillaume
on 18 Sep 2019
Edited: Guillaume
on 18 Sep 2019
What do you want to do with that data?
Personally, I'd go the other way, and put everything into one table with 3 columns: Channel, hBo, state
Most likely, whatever it is you want to do will be easier with just one table than cell array of cell arrays of cell arrays.
CalebJones
on 18 Sep 2019
I wanted to split it because i wanted calculate mean,slope,kurtosis,peak which i will use as my feature set for training my model.
Accepted Answer
Guillaume
on 18 Sep 2019
If you want to perform group statistics, the simplest is indeed to put everything in just one table:
allresults = vertcat(result{:}); %concatenate everything into one table
allresults.channel = repelem((1:numel(result))', cellfun(@height, result)); %and add a channel column
stats = groupsummary(allresults, {'state', 'channel'}, {'mean', @kurtosis}); %not sure what you mean by slope and peak
20 Comments
CalebJones
on 19 Sep 2019
Doesn't do what i expected it to do.
Thanks anyways.
appreciate if you could be me out further.
Guillaume
on 19 Sep 2019
appreciate if you could be me out further
Clearly, I haven't understood what you want, so I need more details in order to help further.
CalebJones
on 19 Sep 2019
![k.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238849/k.jpeg)
If you have a look at this image. Like wise even i have extracted HbO which is oxy hemoglobin and HbR which is deoxy Hemoglobin.
And in my dataset is a task like rest active above image shows rest and tap.
Above image plots only 1 channel signal , likewise there at 15 channels.
My dataset(attached above) has calculated HbO Values and labeled things as rest and active for each channel.
So now i wanted to train my model such that it predicts rest and active states with decent accuracy using features like slope,mean,kurtosis,skewness,etc.
Guillaume hope now you get a gist of whats happening?
Thank you
Guillaume
on 19 Sep 2019
If it's stat per each rest and active period you want, then you simply need to add another variable to identify each of these period:
%this is as before:
allresults = vertcat(result{:}); %concatenate everything into one table
allresults.channel = repelem((1:numel(result))', cellfun(@height, result)); %and add a channel column
%now add a variable whose value is identical for each continuous rest/active period:
allresults.eventid = cumsum([1; diff(findgroups(allresults.state)) ~= 0]);
You can now use that variable for grouping:
stats = groupsummary(allresults, {'state', 'channel', 'eventid'}, {'mean', @kurtosis}); %not sure what you mean by slope and peak
You can specify your own statistical function to pass to groupsummary. It will be called once for each distinct group, with the HbO vector of values for each group.
CalebJones
on 19 Sep 2019
It will be called once for each distinct group, with the HbO vector of values for each group.
Could you elaborate with an example?
Is it like @kurtosis_allresult would call---> kurtosis_allresult = kurtosis(allresult,0,'all');
Similarly...
Skweness_allresult = skewness(allresult);
slope_allresult = diff(allresult);
Guillaume
on 19 Sep 2019
Edited: Guillaume
on 19 Sep 2019
No, I've given you an example above:
stats = groupsummary(allresults, {'state', 'channel', 'eventid'}, {'mean', @kurtosis}); %not sure what you mean by slope and peak
Since I don't have the stats toolbox (with kurtosis), I can't show you the result for that, but here is an example with a made-up function:
function s = slope(values)
%fit a linear polynomial to the data, and return the slope of the line
if ~isempty(values)
p = polyfit(1:numel(values), values', 1);
s = p(1);
else
s = NaN;
end
end
and using it on your data:
%merge all into one table with extra columns:
allresults = vertcat(result{:}); %concatenate everything into one table
allresults.channel = repelem((1:numel(result))', cellfun(@height, result)); %and add a channel column
allresults.eventid = cumsum([1; diff(findgroups(allresults.state)) ~= 0]);
%get stats: mean and slope
stats = groupsummary(allresults, {'state', 'channel', 'eventid'}, {'mean', @slope}); %not sure what you mean by slope and peak
stats.Properties.VariableNames{end} = 'slope_HbO'; %better variable name that what matlab autogenerate
head(stats) returns:
>> head(stats) %first 8 rows
ans =
8×6 table
state channel eventid GroupCount mean_HbO slope_HbO
______ _______ _______ __________ _________________ ___________________
active 1 2 200 -7.67231127895478 -0.091165802772305
active 1 4 200 35.5962270614642 0.076534807760347
active 1 6 197 -19.7591859046008 0.252152452160729
active 2 8 200 -15.9692390786319 0.235640586381289
active 2 10 200 20.5796697869384 -0.0664683822345007
active 2 12 197 -17.5921264661321 0.200172452731579
active 3 14 200 7.17042032876279 -0.0507776796191182
active 3 16 200 12.637946834818 -0.218112096344478
CalebJones
on 20 Sep 2019
Guillaume Thanks a lot....
But one question why are channels repeating? number of Rest and Active per channel add up to the correct value which is 1197 which is correct.
But could you explain why ?
CalebJones
on 20 Sep 2019
Edited: CalebJones
on 20 Sep 2019
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238976/image.jpeg)
Okay say i used only mean and slope for training i should be excluding eventid and channel right?
Obvioulsy state should be my response (rest or active).
And run the classifier with Groupcount, mean and slope right?
Guillaume
on 20 Sep 2019
why are channels repeating
You get one row per unique combination of channel, state and eventid.
With regards to the classification, I'm afraid I can't help with that, I don't have the toolbox. If you don't care which channel the data come from, I assume you should remove it from the classification. You obviously don't care about the eventid anymore, its only purpose was to allow grouping into consecutive identical states.
CalebJones
on 20 Sep 2019
I analysed it step by step.Now it makes sense.Your a genuis mate.
I was looping to do all this and it was driving me nuts.Your code was just consise and to the point.
CalebJones
on 23 Sep 2019
I was calculating variance and its throwing this error----
stats = groupsummary(allresults, {'state', 'channel', 'eventid'}, {'mean', @slope_HbO,@variance_HbO});
the function variance_HbO contains this----
function vari = variance_HbO(values)
%fit a linear polynomial to the data, and return the slope of the line
if ~isempty(values)
v = var(1:numel(values), values', 1);
vari = v(1);
else
vari = NaN;
end
error-------
Error using groupsummary (line 522)
Unable to apply method 'fun2' to data variable HbO.
Caused by:
Error using var (line 192)
W must be a vector of nonnegative weights, or a scalar 0 or 1.
Guillaume
on 23 Sep 2019
You don't need a separate function for that. var is supported by groupsummary:
stats = groupsummary(allresults, {'state', 'channel', 'eventid'}, {'mean', @slope_HbO, 'var'});
Unless you do want a weight of 1. In which case if you use a separate function it's as simple as:
function vari = variance_hBO(values)
vari = var(values, 1);
end
You could also use an anonymous function
stats = groupsummary(allresults, {'state', 'channel', 'eventid'}, {'mean', @slope_HbO, @(v) var(v, 1)});
CalebJones
on 23 Sep 2019
Thanks that worked...
I was trying to include peak as well.
stats = groupsummary(allresults, {'state', 'channel', 'eventid'}, {'mean', @slope_HbO, @(v) var(v, 1), @(p) findpeaks(p)});
but it was throwing an error,
Error using groupsummary (line 522)
Unable to apply method 'fun3' to data variable HbO.
Caused by:
Error using findpeaks
Expected Y to be nonempty.
If I try k = findpeaks(allresults.HbO); then size of k is 229x1 so will that be an issue while grouping?Is there a way around this ?
Guillaume
on 23 Sep 2019
You have two issues there.
As an implementation detail, groupsummary first call the function with an empty vector (not sure why) and findpeak doesn't like that. That's the error you're getting.
The second issue is that groupsummary wants scalar output out of the function and that's not going to be the case with findpeaks.
You could fix both problems with this function (untested):
function out = my_findpeaks(values)
if isempty(values) %function may be called with empty vector by groupsummary, which is a problem for findpeaks
out = {}; %return scalar cell with nothing in it
else
out = {findpeaks(values)}; %wrap output of findpeaks in a scalar cell
end
end
But do you really want to return all the peaks? it may be better to extract just one or two peaks from the data in the above function.
CalebJones
on 23 Sep 2019
function out = my_findpeaks(values)
if isempty(values) %function may be called with empty vector by groupsummary, which is a problem for findpeaks
out = {}; %return scalar cell with nothing in it
else
out = {findpeaks(values)}; %wrap output of findpeaks in a scalar cell
end
end
Unfortunatly, this doesn't work.
And yes perhaps one or two peaks would be better than all.
CalebJones
on 23 Sep 2019
Error using groupsummary (line 522)
Unable to apply method 'fun3' to data variable HbO.
Caused by:
Error using findpeaks>parse_inputs (line 206)
Data set must contain at least 3 samples.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)