Logical Indexing: deleting empty array

조회 수: 2 (최근 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
  댓글 수: 3
Eric
Eric 2013년 9월 24일
thanks for the suggestion
Matt J
Matt J 2013년 9월 24일
편집: Matt J 2013년 9월 24일
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.

댓글을 달려면 로그인하십시오.

답변 (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에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by