Search code examples
matlabexport-to-exceldata-import

Matlab data pull and save


What I'm attempting to do is simple: Given an excel dataset and user defined inputs, open the associated excel file, pull the associated information for the user inputted information and save this information in a separate excel file.

I've already developed a list of values and the program recognizes user input with associated checks. I'm stuck on getting Matlab to use this information to open the correct dataset, I don't know how to get Matlab to pull a row/column in excel with a silent open and I don't know how to get it to save that data into a separate excel file. Any help would be appreciated, thank you.


Solution

  • Consider using the functions readtable, and writetable if you have a recent MATLAB (anything more recent than R2013b). The readtable function will 'silently' read data from a specific worksheet in an Excel file into a MATLAB table variable. From there you can 'query' the table to find the specific rows you want and write the result to a new excel table with writetable.

    Using readtable, you can specify the range of data with the parameters sheet and range.

    requested_data = readtable(excel_file, ...
        'sheet',input_sheet_name, ...
        'range',input_data_range);
    

    and write the data to another Excel file with

    writetable(requested_data,ouput_excel_file, ...
    'sheet',output_sheet_name, ...
    'range',output_data_range);
    

    Note: Remember to set the values for excel_file, input_sheet_name, input_data_range, output_excel_file, output_sheet_name, and output_data_range before running above commands.

    Querying the table to access data in your table. One way would be to use ismember as in this answer.

    Finally, use writetable to store the values.

    See also: sheetnames, detectImportOptions, and SpreadsheetImportOptions