Explode cell that are into another cell

5 Ansichten (letzte 30 Tage)
NirE
NirE am 9 Jan. 2019
Kommentiert: NirE am 22 Jan. 2019
I have a cellarray data like in the picture : each cell contains cellarray of strings
I am opening all all the cells with this way
counter=0;
for ind=1:length(data)
tmp=cell2str(data{ind,1});
for k=1:size(tmp,1)
counter=counter+1;
tmp2=textscan(tmp(k,:),'%s%s%s%s%s%[^\n\r]','Delimiter', ' ');
for j=1:6
if isempty(tmp2{j})==0
Raw(counter,j)=tmp2{j};
end
end
clear tmp2 j
end
clear k tmp
end
The results is correct but is there a better/faster way to do it ?
Using parfor, or other technics
Thank you in advance
  9 Kommentare
Jan
Jan am 9 Jan. 2019
Note: Omit the useless clear commands. They will waste time only here.
dpb
dpb am 9 Jan. 2019
Well, no...helping would be to show us what you really, really want instead of just describing it that we can't reproduce.
Where did you find the function? SHOW us!!!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 9 Jan. 2019
Start with a pre-allocation:
Len = cellfun('prodofsize', data);
Raw = cell(sum(Len), 6);
c = 0;
for ind = 1:numel(data)
tmp = data{ind};
for k = 1:numel(tmp)
c = c + 1;
tmp2 = strsplit(tmp{k}, ' ');
for j = 1:numel(tmp2)
Raw{c, j} = tmp2{j};
end
end
end
I cannot open your MAT file currently, so I guess, what it might contain. I guessed also, that cell2str can be avoided by scanning the cell element directly. I assume that Raw should be a cell array. All these assumptions can be wrong. If you post a small input as code and the wanted output, less guessing is required.
  5 Kommentare
Jan
Jan am 22 Jan. 2019
Yes, a parallelizaion should be very straigh forward. Did you try it?
NirE
NirE am 22 Jan. 2019
Will try and tell you how it increase or not

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

dpb
dpb am 9 Jan. 2019
Bearbeitet: dpb am 9 Jan. 2019
OK, I overlooked the regular expression in the format string that sucks up all of those extra blanks at the end of the odd-man-out records...
To dereference the cell content in each cell requires two levels snce textscan isn't cell-string aware. split doesn't cut it here because there's not a unique delimiter that defines the fields desired; hence the above...
You can try the following and see if the lack of preallocation shows up as a performance hit with the size; oftentimes it'll fool you and not be too bad...
fnTS=@(s) textscan(s,'%s%s%s%s%s%[^\n\r]','Delimiter', ' ');
res=[];
for i=1:length(data)
res=[res;cellfun(fnTS,data{i},'uni',0)];
end
res=cat(1,res{:});
The above yields a 64x6 cell array...
I'd have to think of the bestest way to be able to build the array directly w/o the intermediary second cell array to not be dynamically catenating the output.
ADDENDUM:
res(cellfun(@isempty,res))={''};
>> string(res)
ans =
11×6 string array
"1" "EventDataLogNewFile" "DataEventTime" "TypeSecondsSinceEpoch" "1546725641" ""
"1" "EventDataLogNewFile" "DataEventTime" "TypeFormattedDate" "Sun" "Jan 6 00:00:41 2019"
"1" "EventDataLogNewFile" "DataReportingSubsystem" "TypeString" "datalogger" ""
"1" "EventDataLogNewFile" "DataInstrumentID" "TypeString" "00:01:05:19:CF:30" ""
"1" "EventDataLogNewFile" "DataEntityName" "TypeString" "mc16" ""
"4" "EventREAD" "DataEventTime" "TypeSecondsSinceEpoch" "1546725657" ""
"4" "EventREAD" "DataEventTime" "TypeFormattedDate" "Sun" "Jan 6 00:00:57 2019"
"4" "EventREAD" "DataReportingSubsystem" "TypeString" "pc" ""
"4" "EventREAD" "DataEntityID" "TypeString" "Dev_CLPC_PressureGauge1" ""
"4" "EventREAD" "DataReading" "TypeUnitLessNumber" "34729" ""
"4" "EventREAD" "DataEventDuration" "TypeSec" "0.000686859" ""
>>
for just doing the first two elements in the for...end loop instead of all for brevity.

Luna
Luna am 9 Jan. 2019
I was assuming the same 64x9 cell. Here is my solution gives the same result with Jan's:
cellArray = cellfun(@(x) strsplit(x(:,:),' '), vertcat(data{:}), 'UniformOutput',false);
for i = 1:numel(cellArray)
for j = 1:numel(cellArray{i})
raw{i,j} = cellArray{i}{j} ;
end
end

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Produkte


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by