How to determine the end of an array you are putting data into in an excel file?

5 views (last 30 days)
Hello,
So I have a code that is taking some raw data from s2p files and crunching the numbers and then outputting the results into columns in an excel file. The following code will place the "S21_ang" value into the g column from the row 2 all the way to row 810. However, there are not always 800 values. Sometimes there are 200, 400 and sometimes 1600. So I wanted to see if there was a way to code it so that I do not have to manually input the limit and instead it will go until there are no more values.
xlswrite([fname,'.xlsx'],S12_ang,'Sheet1','g2:g810');
  3 Comments
Jesus Perez
Jesus Perez on 18 Jul 2018
Hi Paolo,
Thank you, it seems like your solution should help, however, it I am trying to play around with it and it won't run an example in the command window.
It tells me 'Undefined function or variable 'xlscol'
So I am unable to get it going.
Do you perhaps know why it won't recognize that function?
Paolo
Paolo on 18 Jul 2018
Only reason as to why it would give you that error is because it can't find the function. Did you place the file in your working directory?

Sign in to comment.

Accepted Answer

dpb
dpb on 18 Jul 2018
Edited: dpb on 18 Jul 2018
Read the doc (carefully)... :)
...
RANGE Character vector or scalar string that specifies a rectangular portion of
the worksheet to read. Not case sensitive. Use Excel A1 reference style.
* If you specify a SHEET, RANGE can either fit the size of
ARRAY or specify only the first cell (such as 'D2').
...
So, just use
xlswrite([fname,'.xlsx'],S12_ang,'Sheet1','G2');
or wherever is the target. Ensure the vector/array is in the shape you want in the spreadsheet, though.
Alternatively, you can dynamically calculate the range; I built a couple utility functions that translate to/from Excel ranges way back when, I think maybe there are some in ML itself now.
  5 Comments
dpb
dpb on 18 Jul 2018
Really can't do anything with images but on
xlswrite(outputfile,S11_norm.',fname,'B');
you asked about syntax and, yes, there is a syntax error in that you didn't specify the starting cell; you specified only the column. If you want to start in a specific cell, you must identify that cell specifically as does my example.
And, of course, the data have to be organized such that the upper LH position of the array is to go into that cell and everything flow down and to the right from there is where you want it to land.
I don't know about the sheet names being in a struct; I've never tried that--I'm pretty sure you would have to dereference and only pass one specific sheet name with each call (altho I've not tried nor researched the doc further on the question, I'd be surprised if xlswrite were vectorized that way).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!