Exporting the Data to Excel

Exporting data to a medium that users can manipulate is very desirable. Since many people work with Excel, adding a feature to export data to Excel can come in quite handy. Lets see how we can export data to Excel. Add the following code to export_pushbutton_Callback:

%if the data hasn't been processed yet, 
%nothing happens when this button is pressed
if (handles.processDataCompleted == 0)
    return
end
 
saveDataToExcel(handles.data,handles.legendData);

Add this code as its own separate m-file, or add it to the data-processing-tool.m file.

function saveDataToExcel(data, fileNames)
 
%stores savepath for the phase plot
[filename, pathname] = uiputfile({'*.xls','Excel (*.xls)'},'Save Data to Excel File','default');
 
%it is assumed that the frequency range is the same for all the data sets
magnitudeData = [data{1}(:,1)];
phaseData = [data{1}(:,1)];
 
for x = 1:length(fileNames)
    magnitudeData = [magnitudeData data{x}(:,2)];
    phaseData = [phaseData data{x}(:,2)];
    magDB{x} = 'Mag (dB)';
    phaseDegrees{x} = 'Phase(Degrees)';
end
 
saveFileName = fullfile(pathname, filename);
xlswrite(saveFileName,['Data File' fileNames],'Magnitude Data','A1');  
xlswrite(saveFileName,['Frequency (GHz)' magDB],'Magnitude Data','A2');
xlswrite(saveFileName,phaseData,'Magnitude Data','A3');
 
xlswrite(saveFileName,['Data file: ' fileNames],'Phase Data','A1');  
xlswrite(saveFileName,['Frequency (GHz)' phaseDegrees],'Phase Data','A2');
xlswrite(saveFileName,phaseData,'Phase Data','A3');
 
deleteEmptyExcelSheets(saveFileName);

And to erase those empty sheets when the Excel file is created, we can use the following function that is discussed in this post.

function deleteEmptyExcelSheets(fileName)
%this function erases any empty sheets in an excel document
%the input fileName is the entire path of the file
%for example, fileName = 'C:\Documents and Settings\matlab\myExcelFile.xls'
 
 
excelObj = actxserver('Excel.Application');
%opens up an excel object 
excelWorkbook = excelObj.workbooks.Open(fileName);
worksheets = excelObj.sheets;
%total number of sheets in workbook
numSheets = worksheets.Count;
 
count=1;
for x=1:numSheets
    %stores the current number of sheets in the workbook
    %this number will change if sheets are deleted
    temp = worksheets.count;
 
    %if there's only one sheet left, we must leave it or else 
    %there will be an error.
    if (temp == 1) 
        break; 
    end
 
    %this command will only delete the sheet if it is empty
    worksheets.Item(count).Delete;
 
    %if a sheet was not deleted, we move on to the next one 
    %by incrementing the count variable
    if (temp == worksheets.count)
        count = count + 1;
    end
end
excelWorkbook.Save;
excelWorkbook.Close(false);
excelObj.Quit;
delete(excelObj);

Once again, you should try testing the GUI to make sure it works, and to test the exporting capabilties. There are many different ways you can export your data to excel. For instance, you can export a separate Excel file for each input file, or you can lump all the magnitude data together into one file, and lump all the phase data into another file. The function that is used in this example does the latter.

Pages: 1 2 3 4 5 6 7