Adding data into existing excel sheet from matlab

I have an excel sheet 'myvalues.xlsx' cosisting of 200 vectors of 127bits each. Now I want to add another vector to my excel sheet which will be placed as the 201th row without changing the existing data. How can I acheive it. Have tried xlswrite but its not working.

2 comentarios

i have the same problem here, could you include your code ? like saving the excel file, and adding it after you execute the file again.
how can we add a row at 100th row and the old 100th row shifts to 101th row?

Iniciar sesión para comentar.

 Respuesta aceptada

Mark Whirdy
Mark Whirdy el 6 de Mayo de 2013
Editada: Mark Whirdy el 6 de Mayo de 2013
It seems that variable "mydata" is of size [1x3] but the size of the range you elected is [1x1] (i.e. a single excel cell rather than a range of cells) in
xlCurrRange = xlsheet.Range(['A' num2str(newRange)]);
% ['A' num2str(newRange)] = 'A301' since newRange appears to be the scalar 1 which is added to 300
I think that instead of 'A301' you want to have 'A301:C301' which is
xlsheet.Range(['A', num2str(newRange),':C', num2str(newRange)]);
which should capture the full [1x3] size of "mydata"
Is this it or have I misunderstood?

1 comentario

Rekha
Rekha el 6 de Mayo de 2013
Works perfectly !
This is exactly what is needed. Guess I wasn't clear in my problem statements earlier..
Thank you Mark...

Iniciar sesión para comentar.

Más respuestas (1)

Mark Whirdy
Mark Whirdy el 3 de Mayo de 2013
Editada: Mark Whirdy el 3 de Mayo de 2013
Hi Rekha
Lots of similar questions on the Matlab Answers here if you search.
Here's one of mine on using the activex server to manipulate excel range values from matlab. In general there's no reason to use xlsread or xlswrite
Use commands like:
mydata = rand(100,3);
xlCurrRange = xlActiveSheet.Range('A201:C300');
xlCurrRange.Value2 = mydata;
google "actxserver" - no shortage of other examples on the web
Alternatively, have a look at Alec de Zegher's approach
All the best
Mark

4 comentarios

Hi Mark,
Though I got some idea with the commands you mentioned and Alec de Zegher's approach, the following is exactly what I need to do.
I have 200 vectors of 127bits each saved in the spreadsheet 'myvalues.xlsx' as mentioned before. Now, I'm obtaining a 127bit row vector in matlab through few previous steps, then compare this 127bit vector with all the other existing vectors in 'mavalues.xlsx', which i'm doing using _ismember _ as follows.
pBitVector=inputVal();
sparseVector=encode(pBitVector);
existValues=xlsread('myvalues.xlsx');%127bit vector to be inserted into spreadsheet
value=ismember(sparseVector,existValues,'rows');
If the value returned is zero then I have to enter the sparseVector into my excelsheet as the 201th row. The implementation is iterated for about 800 more such vectors.
How can I accomplish this.
Thanks in advance.
Mark Whirdy
Mark Whirdy el 4 de Mayo de 2013
Editada: Mark Whirdy el 4 de Mayo de 2013
Hi Rekha
From my understanding of your original question, the crux of the issue is that you want to add write a vector to an excel-sheet beginning at row 201. If the crux of the problem is more the "vector-comparison" algorithm, then I need a lot more detail than the above. But it seems you have a handle on this already.
If the problem is in fact writing to excel (regardless of the content of "sparseVector"), it is easier at my end to ignore the context-specific details and just talk about writing to excel. Could you try the below code [which writes a matrix to Range('A201:C300')] & tell me which part does not work or any part you have a question about?
try
xlApp = actxGetRunningServer('Excel.Application'); % capture existing excel application
catch ME %#ok
xlApp = actxserver('Excel.Application'); % instantiate new excel application
end
xlApp.Visible = 1;
xlWorkbook = xlApp.ActiveWorkbook;
xlActiveSheet = xlWorkbook.ActiveSheet;
xlCurrRange = xlActiveSheet.Range('A201:C300'); %
mydata = randn(100,3);
xlCurrRange.Value2 = mydata;
Hi Mark,
I tried the code given by you. It works perfectly if am writing the mydata generated into the Range ('A201:C300').
Without considering how i'm arriving at the sparseVector or its contents, speaking only with reference to the above code, the problem am encoutering is:
Upon executing your code once my active excel-sheet(which is myvalues.xlsx in this case) will have matrix in the range 'A201:C300'. Before running the code for the second time I change,
mydata=randn(100,3);
to
mydata=randn(1,3);
I want my excel-sheet to get updated with mydata, generated during the second run written into the 301th row and positioned in the range 'A301:C301'. For the third run mydata must be entered into 302th row ('A302:C302') and so on. I have tried to do so, with small modifications in the code provided by you.
xlApp = actxserver('Excel.Application');
xlApp.visible = 1;
%Open the the spreadsheet
xlworkbook = xlApp.Workbooks.Open('myvalues.xlsx');
xlsheet = xlworkbook.ActiveSheet;
mydata=randn(1,3);
data=xlsread('myvalues.xlsx');
%Determine last row
last=size(data,1);
newRange=last+1;
xlCurrRange = xlsheet.Range(['A' num2str(newRange)]);
xlCurrRange.Value2 = mydata;
%Save and Close the Excel File
invoke(xlworkbook,'Save');
invoke(excelApp,'Quit');
delete(excelApp);
The problem is only the first element in mydata(i.e.mydata(1,1)) is being written into excel-sheet during each run but not the entire value.
Not able to figure out what's missing.
How can this be corrected.

Hi Rekha Try using a while loop to update the range after writing each row/column into the excel file

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 3 de Mayo de 2013

Comentada:

el 19 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by