views:

722

answers:

1

I am generating an Excel file through MATLAB and I have empty cells in the middle of it. Here is the code I am using to initialize an empty matrix:

newfile = cell(5,5);
[newfile{:}] = deal('');
[newfile{:}] = deal(' ');

I then do some processing and write the data to a file using XLSWRITE. The spreadsheet ends up having some empty cells and some data. Is there a command by which I can delete the empty rows, without effecting the rows which have data in them?

Another question:

Is there a way to append onto this Excel file? I run this file everyday and would like to automatically add data rather than manually copying and pasting everyday.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Addition to above question:

If my output file looks like

1.8 3 -4.7

1.87 3.9 -5.8

1.84 2.8 -4.3

2 3.2 -4.7

2 3.7 -6
2 3.6 -5.2
3 4.3 -6.5

13 4.2 13.1

12 3.5 -6.3

1.65 1.9 -4.4

So how do i delete the empty rows when its not in cell as cellfun does not work for this

+1  A: 

If I understand the first part of your question right, you want to remove any rows in your newfile cell array that contain only empty cells. You can do this using the function CELLFUN to find empty cells, using the function ALL to find the rows that contain all empty cells, and then using logical indexing to remove those rows:

>> newfile = cell(5,5);  %# Empty cell array
>> newfile(:) = {''};    %# A different way to assign values without DEAL
>> newfile([1 2 4 5]) = {'a'}  %# Make some non-empty cells

newfile = 

    'a'     ''     ''     ''     ''
    'a'     ''     ''     ''     ''
     ''     ''     ''     ''     ''
    'a'     ''     ''     ''     ''
    'a'     ''     ''     ''     ''

>> emptyIndex = cellfun('isempty',newfile)  %# Logical index of empty cells

emptyIndex =

     0     1     1     1     1
     0     1     1     1     1
     1     1     1     1     1
     0     1     1     1     1
     0     1     1     1     1

>> newfile = newfile(~all(emptyIndex,2),:)  %# Removes 3rd row

newfile = 

    'a'     ''     ''     ''     ''
    'a'     ''     ''     ''     ''
    'a'     ''     ''     ''     ''
    'a'     ''     ''     ''     ''

EDIT: If you initialize your cell array newfile with blank spaces (' ') instead of empty strings ('') you will have to change the way you compute emptyIndex in the above code. The CELLFUN argument 'isempty' should be changed to @(x) strcmp(x,' ') to get the same results.



With respect to your second question, you can specify in your call to XLSWRITE the sheet and row/column range in the file where you want to add your data. If you add to the file once per day, the easiest solution may be to add a new sheet with the date as the name. The following writes the data in M to the file fileName, adding a new sheet that is named with the current date and time:

xlswrite(fileName,M,datestr(now));
gnovice
for adding to the excel file. I have 4 files and i name them as M_2010_03_11.xls , N_2010_03_11.xls and so on.How do i merge to a one file by name lets say M.xls , N.xls by your method, I cannot call by row/column range as they are diff each day and if i manually look , i might as well copy and paste ( like i am doing direcly onto excel manually )
AP
@AP: I'm not totally sure what you mean. It sounds like you already have a bunch of separate Excel files that you want to merge into one, whereas my solution above is assuming you already have one file and you're adding a new days worth of data to it as a new sheet. To combine your existing files, I guess you could loop over them, calling XLSREAD to get the raw data (i.e. `[junk,junk,raw] = xlsread(fileName);`) and then writing the raw data to separate sheets in one file.
gnovice
@gnoice , No you understood me right at the first time. I run this program everyday based on my daily files. And i generate 4 files . It would be good , if the new 4 files gets appended as i run the program rather than i manually change the names everyday and manully merge them to the previous day's file.
AP
@AP - Use `xlsread` to read from the old files, append the new data, and then call `xlswrite`, for the second problem. This is (essentially) what gnovice suggested in his comment.
mtrw
@gnoice : Thanks a lot buddy, that helped me understand this better. You are awesome!!
AP