Search code examples
excelvbamatlabcomactivex

Paste Special Transpose Syntax in Matlab using ActXServer


I am working on a code in Matlab that will open excel spreadsheet, copy a certain range, and paste it in a new sheet transposing my range in the process. I am completely stuck on the PasteSpecial method and cannot figure out how to make it transpose my data. I've tried everything I could think of: tried VBA-like syntax (Transpose=True), tried (Transpose, 1), tried ([],[],[],1), tried obj.Transpose(with all kinds of variations in the brackets)... and all kinds of other stuff to no avail. Please help me if anyone had done this before. Below if my simplified code in case it's needed. Thank you in advance!

   Excel = actxGetRunningServer('excel.application'); 
   set(Excel, 'Visible', 1);

   Workbooks = Excel.Workbooks;
   Workbook = Excel.Workbooks.Open('C:\Users\...test.xlsx');
   curr_sheet = get(Workbook,'ActiveSheet');
   rngObj = ('A1:C3')
   rngObj.Copy
   Sheets = Excel.ActiveWorkBook.Sheets;
   new_sheet = Sheets.Add;
   new_sheet.PasteSpecial; %This is where I am stuck!

Solution

  • The documentation for PasteSpecial has four input arguments to indicate the parameters of the paste operation. As you can see, the fourth option indicates whether to transpose the data or not.

    new_sheet.PasteSpecial(NaN, NaN, NaN, true);