Question

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

Was it helpful?

Solution

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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top