Getting Matlab to find successive empty columns on Excel and write data in them

7 Ansichten (letzte 30 Tage)
How can I get my Matlab script to find an empty column of the Excel spreadsheet and write some calculated values in that column?
To elaborate and start off, I've managed to write a simple script:
filename = 'testfile.xlsx';
date_and_name = {'13/03/2015'; 'lulu'};
sheet = 'data1';
xlRange = 'G8';
[B C] = xlsread(filename,sheet,xlRange);
if isempty(C)
xlRange1 = 'G8';
xlswrite(filename,date_and_name,sheet,xlRange1);
elseif ~isempty(C)
xlRange1 = 'H8';
xlswrite(filename,date_and_name,sheet,xlRange1) ;
end
Firstly, I got Matlab to use xlsread and check in testfile.xlsx whether G8 is empty, before writing the data at column G in G8, otherwise it will write the data (date_and_name) in column H at H8. However, if cell H8 is not empty, how can I edit the above Matlab script further to check whether cell I8 is empty and then write the data on it, and to do the same for J8 (if I8 is not empty), K8 (if J8 is not empty) etc in successive columns of row 8?
I will need an expanded range of columns from G to ZZ in row 8, i.e. from G8 to ZZ8.
I'm a greenhorn in programming and still learning. I also know that there's actxserver to consider but I'm not familiar with associated commands like xlToRight etc. By the way I cannot edit the spreadsheet because it's a standard template used by my company.
A big thank you in advance to anyone who can solve my problem! If my method is not good enough and you have a better way to solve it (like using actxserver), do guide me along.
Thank you!
Cheers, Gareth
  5 Kommentare
Geoff Hayes
Geoff Hayes am 14 Mär. 2015
Gareth - why not just read in a range of data, from G8:Z8, and see which element is empty? Else if you are going to continue as above, you would need to do some sort of iterative approach by reading in G8, H8, I8, etc.
dpb
dpb am 14 Mär. 2015
Bearbeitet: dpb am 14 Mär. 2015
In re-reading, I may have misunderstood the problem...is the question one as simple as simply adding a column to the end of existing worksheet? I had thought from the first reading it was to insert a column into a blank area which might then also cause a rearrangement to be needed of others but on re-read I'm not so sure...
Either way, as suggested earlier and as Geoff also suggests, simply reading all the sheet data section and appending the column if that's all that's needed or rearranging and inserting and re-writing if there are other restrictions on the format/location of particular columns is "deadahead" and [essentially] trivial; don't need to worry about finding the empty column in the sheet a priori.
Unless the sheet is huge (doesn't sound as tho is so) or the process is buried in some iterative loop or somesuch it shouldn't be a performance issue and is surely better than repetitively reading/writing with xlsread/write; if that's the case then ActiveX is the way to go. But, that gets into really being an Excel problem, not Matlab with the interface API.
Clarify the actual requirements more thoroughly; often an example is the easiest way; attach a sample file and gives the desired end product description.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by