Writing a cell array to excel but skipping certain values

I am currently working on code that will write values from a x*1 cell array to an excel file. I need to write each 15th value to an excel file (this I have managed to do).
This is what I am using to write each 15th cell in the cell array: x_new=X(15:15:end); xlswrite('test.xlsx', X_new)
Now, what I need help with is to some code to this that will let me be able to skip certain values of this x*1 cell array. I want to write each 15th value, but I want to skip all 0 values. However, I cannot remove the 0 values before I write to excel. I need to write each 15th value to excel, but if it is ever 0 (if number cell number 15, 30 or 45 is zero) then that should be skipped and the next value (not zero) should be written instead, before it goes back to writing each 15th cell.
Is this clear? I do not have much experience with matlab, and I am using it to collect data for my master thesis.

Answers (1)

I think it is clear what you want to do- I don't have MATLAB open, so forgive minor syntax issues.
1. Step one extract the every 15th points
x_new = X(15:15:end);
x_new = cell2mat(x_new); %changing into double array
x_idx = ((1:length(x_new))) * 15; % index in ref to x
2. Find if any zeros exist
ind = find(x_new==0);
3. Replace existing values with idx + 1 values
x_new(ind) = [X{x_idx(ind)+1}];
4. Write out x_new to excel
** Edited **
New version of code including check if x_new is a cell array
%%New version of the code
X = num2cell(rand(500,1));
X{15} = 0;
X{135} = 0;
X{300} = 0;
x_new = X(15:15:end);
% Make sure you are working with a cell array
if iscell(x_new)
x_new = X(15:15:end);
x_new = cell2mat(x_new); %changing into double array
x_idx = ((1:length(x_new))) * 15; % index in ref to x
ind = find(x_new==0);
x_new(ind) = [X{x_idx(ind)+1}];
else
disp('Your variable not a cell')
end

8 Comments

Okay, I tried this at school, and the following error message popped up:
>> x_new = cell2mat(x_new)
??? Cell contents reference from a non-cell array object.
and
>> x_new(ind) = [X{x_idx(ind)+1}]
??? Cell contents reference from a non-cell array object.
Error in ==> cell2mat at 44
cellclass = class(c{1});
which I guess refers to the same problem.
I worked around it by writing X to an excel file first, and then reading that back into matlab. Which is, needless to say, a really clumsy and labourus task.
Anyways, after I had followed your steps, and everything worked, x_new still contained cells with 0 value. Sometimes there are several 0 (perhaps between 50 zeroes) in a row in the original X cell array. Could that be the problem?
Hi Magnus,
It should be work regardless of how many zeros you have.
Does your original cell array only contain numbers or are their strings as well? Could you do a whos on your cell array variable for me?
Cheers,
Sarah
Also x_new should not be a cellarray any more.
%% New version of the code
X = num2cell(rand(500,1));
X{15} = 0;
X{135} = 0;
X{300} = 0;
x_new = X(15:15:end);
% Make sure you are working with a cell array
if iscell(x_new)
x_new = X(15:15:end);
x_new = cell2mat(x_new); %changing into double array
x_idx = ((1:length(x_new))) * 15; % index in ref to x
ind = find(x_new==0);
x_new(ind) = [X{x_idx(ind)+1}];
else
disp('Your variable not a cell')
end
Whos on x:
Name Size Bytes Class Attributes
X 31992x1 255936 double
I tried several times with the new code, and in your example it works fine, but not with my own X. Perhaps this is due to it not being a cell? However, even if I make X into a cell, it still does not seem to work properly.
I mean, it works, but there are still 0 values in X, which is what I need to get rid off =)
Also, if I change your example to this:
X = num2cell(rand(500,1));
X{15} = 0;
X{135} = 0;
X{300} = 0;
X{301} = 0;
X{309} = 0;
X{498} = 0;
x_new = X(15:15:end);
Then a zeroe value will remain after I run this:
if iscell(x_new)
x_new = X(15:15:end);
x_new = cell2mat(x_new); %changing into double array
x_idx = ((1:length(x_new))) * 15; % index in ref to x
ind = find(x_new==0);
x_new(ind) = [X{x_idx(ind)+1}];
else
disp('Your variable not a cell')
end
I assume that the value next to the zero is not a zero. If it is a zero, you need to do a check and advance one more.
I managed, with help, to solve my problem, thank you for your answer =)

Sign in to comment.

Categories

Tags

Asked:

on 20 Feb 2012

Edited:

on 22 Oct 2013

Community Treasure Hunt

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

Start Hunting!