Arrange cell matrix and get the sum

1 view (last 30 days)
Damith
Damith on 12 Mar 2015
Edited: Stephen23 on 18 Mar 2015
Hi,
"out" cell matrix has 1x365 cells. I have few questions about dealing with cells.
1) I need to delete cells from 361 to 365 in the "out" cell matrix. How can I do that in MATLAB?
2) After deleting, I need to get the sum like this. For example, from cell 1 - 8.
cell1 cell2 .....cell8 sum
0.0 0.1 0.0 0.1
0.0 0.2 0.5 0.7
0.0 0.1 0.1 0.2
.
.
For example,
for ii=1:8:360
sumout=sum(out{1,ii}{1,3});
end
But, this does not give the sum for all rows. Can someone help me?
Thanks in advance.

Accepted Answer

Stephen23
Stephen23 on 12 Mar 2015
Edited: Stephen23 on 12 Mar 2015
Putting lots of scalar values in a cell array is a waste of MATLAB's operation vectorizing abilities and this is why you are finding this task so difficult. I will use a simple numeric array, as this makes the doing these kind of operations much easier. Putting things in cell arrays is what you do when you have to, not for some trivial scalar values like this.
>> A = rand(365,1);
>> B = reshape(A(1:360),[],8);
>> sum(B,2)
ans =
4.4687
3.694
4.5899
3.4787
...
3.5861
3.7248
3.8888
5.2515
This code takes the first 360 values in the numeric array, arrange them into a matrix where each row has eight values. It then sums each row to give the final values in a 45x1 vector. And see how much easier it is to work with numeric arrays rather than cell arrays! You can convert the cell array to a numeric array using cell2mat(X), where X is the cell array.
  8 Comments
Damith
Damith on 17 Mar 2015
Edited: Damith on 17 Mar 2015
@Stephen,
Please disregard my original post as I used arbitrary values just for the demonstration purpose. That's my bad. Please see the attached TRMM_1998_01_0100_newntcl.csv file.
Each csv file has 3 columns and 51681 rows. That's exactly what I want. I need to store the remaining values too and to get the daily sum.
For example daily sum (1998 Jan 01) for 51681 lat, lon points =
TRMM_1998_01_0100_newntcl.csv(:,3) +
TRMM_1998_01_0103_newntcl.csv(:,3) +
TRMM_1998_01_0106_newntcl.csv(:,3) +
TRMM_1998_01_0109_newntcl.csv(:,3) +
TRMM_1998_01_0112_newntcl.csv(:,3) +
TRMM_1998_01_0115_newntcl.csv(:,3) +
TRMM_1998_01_0118_newntcl.csv(:,3) +
TRMM_1998_01_0121_newntcl.csv(:,3)
Stephen23
Stephen23 on 18 Mar 2015
Edited: Stephen23 on 18 Mar 2015
In MATLAB addition of two vectors is an element-wise operation: because the third-column of each of file TRMM_1998_01_0XXX_newntcl.csv is a 51681x1 size numeric vector, the end result will also be a single 51681x1 vector. If this is what you want, then you can simply add the data in a loop. If not, then you need to specify how the data should be handled.
This code, based on your earlier comment, shows how you can get the data during each iteration:
filePattern = fullfile(myFolder, '*.csv');
csvFiles = dir(filePattern);
for k = 1:length(csvFiles)
fid(k) = fopen(fullfile(myFolder,csvFiles(k).name));
out{k} = textscan(fid(k),'%s%s%f','delimiter',',');
fclose(fid(k));
dat = out{k}{3};
end

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 12 Mar 2015
% Make up some data that seems to be like yours
rng 'default'
out = num2cell(rand(1,365));
% Convert from cell array to numeric
out_numeric = cell2mat(out);
% Trim the unwanted
trimmed_out = out_numeric(1:360);
% Reshape to 45x8
reshaped_out = reshape(trimmed_out,45,8);
% Sum
summed_out = sum(reshaped_out,2);
  1 Comment
Damith
Damith on 12 Mar 2015
Edited: Damith on 13 Mar 2015
my out is a cell matrix which I obtained through this:
myFolder = 'C:\Users\Desktop\test';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.csv');
csvFiles = dir(filePattern);
for k = 1:length(csvFiles)
fid(k) = fopen(fullfile(myFolder,csvFiles(k).name));
out{k} = textscan(fid(k),'%s%s%f','delimiter',',');
fclose(fid(k));
end
cell2mat(out) does not work for me. I get an rrror message: (See the image of my cell matrix)
Error using cell2mat (line 52)
Cannot support cell arrays containing cell arrays or
objects.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!