Logical Indexing: deleting empty array

1 次查看(过去 30 天)
Eric
Eric 2013-9-24
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

回答(1 个)

Walter Roberson
Walter Roberson 2013-9-25
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 个评论
Eric
Eric 2013-9-25
I apologize for the typo. This line of code is written as:
tnum = tnum(~cellfun(@isempty,tnum));

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by