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.

回答 (1 件)

Sarah Wait Zaranek
Sarah Wait Zaranek 2012 年 2 月 20 日

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 件のコメント

Magnus
Magnus 2012 年 2 月 21 日
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?
Sarah Wait Zaranek
Sarah Wait Zaranek 2012 年 2 月 21 日
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
Sarah Wait Zaranek
Sarah Wait Zaranek 2012 年 2 月 21 日
Also x_new should not be a cellarray any more.
Sarah Wait Zaranek
Sarah Wait Zaranek 2012 年 2 月 21 日
%% 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
Magnus
Magnus 2012 年 2 月 23 日
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 =)
Magnus
Magnus 2012 年 2 月 24 日
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
Sarah Wait Zaranek
Sarah Wait Zaranek 2012 年 3 月 8 日
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.
Magnus
Magnus 2012 年 3 月 12 日
I managed, with help, to solve my problem, thank you for your answer =)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Import from MATLAB についてさらに検索

タグ

質問済み:

2012 年 2 月 20 日

編集済み:

2013 年 10 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by