MATLAB: How to search an Excel file for a certain column, then save that entire column

columnsexcelseperate

I am working on a project where columns could be out of order, so I cannot depend on saving data from these columns as they will change. The first row of each column is the name of the data that the column contains. I want to search for the name of the column then save the data from the column.

Best Answer

The table option may work, I personally don't have much experience reading tables. I tend to read excel files using the more common xlsread() command. For finding a specific column based on column title I have used.
[numbers,text,alldata] = xlsread(excelfile);
[headerRow,headerCol] = find(cellfun(@(x)~isempty(strfind(x,'header')),alldata));
By creating the xlsread array results you allow xlsread to input all of the data, instead of just the number results, this allows it to maintain any cells with text.
The array of the second command allows you to find the specific row and column for the matching string, 'header'. The find command is what actually brings up the row and column, while the cellfun command allows all cells within the specified array (datain) to be checked for the condition. If I remember correctly, strfind only works for an array of strings, so the isempty check and cellfun command are necessary to make this work with the mixed data in the datain array.
Related Question