concat/horzcat for cell with data cells of a*b*c sizes
1 view (last 30 days)
Show older comments
Read variables from files
ncvars={'a','b'}; % variables need from files ncvar{1}=a, ncvars{2}=b
prjdir=[datadir 'mentioned path']; % path to directory
dinfo=dir(fullfile(prjdir,'*.nc')); %taking all files with extension .nc
num_files=length(dinfo); % number of files
E1=cell(num_files,1); % defining cell
E2=cell(num_files,1);
% taking a and b from files and putting in E1 and E2 respectively
for K = 1:num_files
file=filenames{K};
E1{K}=ncread(file, ncvars{1});
E2{K}=ncread(file,ncvars{2});
end
Now the question is I want to join/concat/horzcat E1 and E2.
Where I have E1 and E2 of 2*1 cell. (2 is number of files)
When I had E1{1}=180*161 double and E1{2}= 180*146. I used folowing to horzcat:
Etemp=horzcat(E1{:},E2{:}); %Etemp is 180*614 where 614 because 2*(161+146)
E=Etemp(1:end) % E dimention 1*110520 double
Now I have E1{1}=180*161*253 double and E1{2}=180*146*127 double. Same for E2 too.
How can I use horzcat for E1 and E2 in this case??
So that I can have Etemp dimention someting like 180*100008 where because 2*(161*253+146*127)???
0 Comments
Accepted Answer
Jan
on 31 May 2022
E1m = reshape(E1, 180, []);
E2m = reshape(E2, 180, []);
E = cat(2, E1m, E2m);
cat(2, ...) is the same as horzcat().
2 Comments
Jan
on 1 Jun 2022
Edited: Jan
on 1 Jun 2022
Yes.
C = cell(1, 2);
for k = 1:2
C{k} = reshape(E1{k}, 180, []);
end
E1M = cat(2, C{:});
A compact (but not faster) solution:
C = cellfun(@(x) reshape(x, 180, []), E1, 'UniformOutput', 0);
E1M = cat(2, C{:});
I use a new name instead of re-using E1. Letting a variable change its type from cell to double impedes the JIT acceleration. It is not a bug, but faster to avoid this.
More Answers (0)
See Also
Categories
Find more on Entering Commands 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!