Logical Indexing: deleting empty array

6 views (last 30 days)
Eric
Eric on 24 Sep 2013
I have trial names that end with numbers e.g. _1 or _30. The number suffix does not increment by _1, _2, _3 etc because I am only reading a few one minute trials over a thirty minutes of data. If the condition is 30 minutes, then the data from these trials is being stored in a 1x30 structure even though 2-29 is empty because I am only reading in two trials. Without changing the trial name, is there a way that only stores arrays that are populated?
tnum = str2num(fname(jtnum+1:j)); % trial number if there is one
tnum(~cellfun(@isempty,tnum));%keeps only populated cells, I hope
%fname is the name of the trial which vary in length depending on the trial type e.g. condition_1.
%tnum is the trial number e.g. 1, 2, etc
%j is the length of the fname jtnum = j e.g. 14.
%here is how the data is being stored
if dtype{i} == 3% dtype is a signal and i one of many types of signals
subj.(fname)(tnum).(datanames{i}) = M(:,i); % M is nxm input matrix
end%dtype
The second tnum is the line where the error occurs:
<Input #2 expected to be a cell array, was double instead>
Therefore, I added a second line of code,
tnum = mat2cell(tnum)
but got an error saying <Matrix indices must be full double>
If I omit the second tnum, then the code works but I end up with something like this
subj.power_(2)
ans =
ana: []
force: []
but
subj.power_(1)
ans =
ana: [1x1 struct]
force: [1x1 struct]
So, it is these empty structures 2-29 that I want to get rid of and end up with a structure of 1xnumber of trials with data. Any suggestion of how to get rid of the empty array is appreciated.
Eric
  3 Comments
Eric
Eric on 24 Sep 2013
thanks for the suggestion
Matt J
Matt J on 24 Sep 2013
Edited: Matt J on 24 Sep 2013
Yet you chose not to follow it? It would be much easier to read your code if you applied the "{} Code" markup tool to it.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 25 Sep 2013
tnum(~cellfun(@isempty,tnum));%keeps only populated cells, I hope
doesn't do anything with its calculation. You need an assignment statement.
tnum = tnum(~cellfun(@isempty,tnum));%keeps only populated cells, I hope
  1 Comment
Eric
Eric on 25 Sep 2013
I apologize for the typo. This line of code is written as:
tnum = tnum(~cellfun(@isempty,tnum));

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!