views:

173

answers:

2

I had posted a similar question related to plotting data, and now I'd like to know how to handle missing data when outputting data to an Excel file using the XLSWRITE function.

I have two sets of data of different length (described in the question I link to above). I am trying to replace the smaller file with zeroes for the times when the data is missing. This is the code I tried to use:

newfile2 = zeros(144,20);
[ts,ifile1,ifile2] = intersect(file1(:,1),file2(:,1));
newdfile2(ifile2,:) = file2;

Here, I have already converted the column 1 data to a number using DATENUM. So ifile2 is giving me indices of times that are common to both files. The problem is I get this error for line 3:

((??? Subscripted assignment dimension mismatch. ))

This is because the size of file2 is 130-by-20, but my file1 is of size 144-by-20. I also tried a different approach using ISMEMBER to find the missing indices and still got the same error.

Can someone please give me some guidance?

+1  A: 

If I understand your question correctly, you want to copy from file2 to newfile2, and you want to copy the data that has timestamps that exist in file1, and you want newfile2 to be the same size as file1.

newfile2 = zeros(size(file1));
[ts,ifile1,ifile2] = intersect(file1(:,1),file2(:,1));
newfile2(ifile1,:) = file2(ifile2,:);

should do the trick.

mtrw
I am little confused. when i ran my code i did not get the error i wrote above.i have a newer version of matlab than my office. thanks a lot.Your code also works fine on my home matlab version . i need to check to see if it works in the older version in my office or i get the same error.thanks
AP
Another followup question:is there a way to keep those rows empty and not have zero in excel sheet by the matlab code? ( rows on which the smaller file have missing data)
AP
@AP: That depends... How are you writing data to the file?
gnovice
@mtrw: I think you have to add `newfile2(:,1) = file1(:,1);` so that the time-stamps themselves aren't zero.
gnovice
@gnovice: I am using xlswrite to write the file that i am generating 'newfile2'and to read the data file initially i was using textscan
AP
@gnovice - thanks for catching that. @AP - sorry I wasn't around after you asked your followup, looks like gnovice took good care of you :)
mtrw
+1  A: 

One solution that will allow you to output "empty" cells to your Excel worksheet is to make the data a cell array with empty values or blanks instead of zeroes, using one of the following options:

newData = cell(size(fileData1));  %# Each cell is initialized to []
[newData{:}] = deal('');          %# Change the cell contents to an empty string
[newData{:}] = deal(' ');         %# Change the cell contents to a blank

There are then two ways to proceed. If the time-stamps in the smaller file are only ever a subset of the ones in the larger (i.e. there is never a time-stamp in the smaller file that is not in the larger), then you can just use the ISMEMBER function as follows:

t = fileData1(:,1);                      %# Time-stamps from file 1
index = ismember(t,fileData2(:,1));      %# Find index of common time-stamps
newData(:,1) = num2cell(t);              %# Copy time-stamps
newData(index,:) = num2cell(fileData2);  %# Copy file 2 data

However, if there are time-stamps in the smaller file that aren't in the larger, you should instead do the following using the INTERSECT function:

t = fileData1(:,1);                                  %# Time-stamps from file 1
[junk,index1,index2] = intersect(t,fileData2(:,1));  %# Find indices
newData(:,1) = num2cell(t);                          %# Copy time-stamps
newData(index1,:) = num2cell(fileData2(index2,:));   %# Copy file 2 data

The above will discard any time-stamps that are in the smaller file but not in the larger. If you want to include this extra data then include the following additional code (which uses the functions SETDIFF and SORT):

[junk,index] = setdiff(fileData2(:,1),t);           %# Unique time-stamp indices
newData = [newData; num2cell(fileData2(index,:))];  %# Add unique data
[junk,index] = sort([newData{:,1}]);                %# Sort the time-stamps
newData = newData(index,:);                         %# Reorder the data

Now, if you use XLSWRITE to output newData to an Excel file, the padding cells should show up as empty, although the documentation for XLSWRITE has these remarks:

Full functionality of xlswrite depends on the use of the Microsoft Excel COM server. The typical installation of Excel for Windows includes access to this server. If your system does not have Excel for Windows installed, or if the COM server is unavailable, xlswrite:

  • Writes matrix M as a text file in comma-separated value (CSV) format.

  • Ignores the sheet and range arguments.

  • Generates an error if the input matrix M is a cell array.

If your system has Microsoft Office 2003 software installed, but you want to create a file in an Excel 2007 format, you must install the Office 2007 Compatibility Pack.

gnovice
Thanks , i will work on this and let you know . Honestly i am really thankful.
AP
gnovice you are amazing. it works great on my matlab version at home, will let you know tomorrow if it had any problems in the older version i use at work.
AP
Thank you , thank you , thank you. :) :)
AP

related questions