How do I delete empty array among populated arrays?

Hi,
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; the data is written to a .mat file. Without changing the trial name, is there a function in MATLAB that only stores arrays that are populated.
I tried doing this:
tnum = str2num(fname(jtnum+1:j)); % trial number if there is one
tnum = tnum(~cellfun(@isempty,tnum));%keeps only populated cells
But I get an error saying: Input #2 expected to be a cell array, was double instead.
Therefore, I added a second line of code, tnum = mat2cell(tnum) to try and get around the error "error to be cell array..." but got an error saying "Matrix indices must be full double."
Any suggestion is much appreciated.
Eric

6 Comments

Please post a copy of the original error message. The details matter.
This is the original error message
Input #2 expected to be a cell array, was double instead.
Error in import_HOdata_GENERAL_direct_EFworks_v2 (line 190) tnum = tnum(~cellfun(@isempty,tnum));
The description of what you do is not clear. For yourself it is very clear, what "trial names" are, but the readers have to guess all such details. It would be more helpful, if you post Matlab code, which reproduces some typical data. It is not clear, if "fname" from the code are the "trial names" from the description. The type and contents of "jtnum" and "tnum" are not explained also. Therefore too much guessing is required to create an answer.
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.
The purpose of the code is to store data in a structure. The part of the code that I get the error is after the data has been sorted based on condition, type of signal, etc.
tnum = str2num(fname(jtnum+1:j)); % trial number if there is one
tnum = tnum(~cellfun(@isempty,tnum));%keeps only populated cells
the second tnum is the line where the error occurs
if isempty(tnum) == 1 % checks to see if there is not a tnum
tnum = 1;
end
%here is how the data is being stored
if dtype{i} == 3% dtype is a signal and i one of many types of signals e.g. force subj.(fname)(tnum).(datanames{i}) = M(:,i); % M is x m double else ..... end%dtype
I hope that clarifies what is going on in the code.
Thanks, Eric
Thanks for the clarifications.
If fname is a string, str2num replies a number, which is stored in tnum. Then cellfun(@isempty, tnum) does not work, because tnum is not a cell, but a number.
The comment "keeps only populated cells" does not enlighten me concerning the purpose of this line. Because you check the emptiness of tnum in the IF command later, what about simply omitting the the complete "tnum = tnum(~cellfun(@isempty,tnum));" line?
If I omit the line "tnum = tnum(~cellfun(@isempty,tnum)", then the code will run but the output is not what I want. Because in a case were I only have data for trials 1 and 30, trial type is stored as 1x30 structure. However 2-29 are empty i.e.
>> subj.power_(2)
ans =
ana: []
force: []
kin: []
mar: []
but
>> subj.power_(1)
ans =
ana: [1x1 struct]
force: [1x1 struct]
kin: [1x1 struct]
mar: [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.
Thanks, Eric

Sign in to comment.

Answers (1)

You are probably better off not having variables named like this in the first place.

Categories

Find more on Code Execution in Help Center and File Exchange

Asked:

on 16 Sep 2013

Community Treasure Hunt

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

Start Hunting!