Paste 2D array into excel via activex

  • Follow


I've managed to select a range of cells in my workbook and I want to
paste an array into those cells.

Select(Range(ActSheet,'A1:C100'));

I then tried to use
ActCell = get(Spreadsheet1,'ActiveCell');
set(ActCell,'Value',data);

but it didn't work. I assumed the active cell property would cover the
whole range selected, but it doesn't appear so.

If I try
ActRange = get(Spreadsheet1,'Range');
I recieve an error:


??? Invoke Error: Incorrect number of arguments

Error in ==> torquecurveinput8>labelupdate at 264
    ActRange = get(Spreadsheet1,'Range');


How can I successfully paste an array of data without having to run
through each individual cell in the range with nested for loops?
0
Reply Adam 7/21/2010 7:57:44 AM

On Jul 21, 8:57=A0am, Adam Chapman <adamchapman1...@hotmail.co.uk>
wrote:
> I've managed to select a range of cells in my workbook and I want to
> paste an array into those cells.
>
> Select(Range(ActSheet,'A1:C100'));
>
> I then tried to use
> ActCell =3D get(Spreadsheet1,'ActiveCell');
> set(ActCell,'Value',data);
>
> but it didn't work. I assumed the active cell property would cover the
> whole range selected, but it doesn't appear so.
>
> If I try
> ActRange =3D get(Spreadsheet1,'Range');
> I recieve an error:
>
> ??? Invoke Error: Incorrect number of arguments
>
> Error in =3D=3D> torquecurveinput8>labelupdate at 264
> =A0 =A0 ActRange =3D get(Spreadsheet1,'Range');
>
> How can I successfully paste an array of data without having to run
> through each individual cell in the range with nested for loops?

Lines 21-23 of the example at
http://www.mathworks.com/support/solutions/en/data/1-17PWC/index.html?solut=
ion=3D1-17PWC
work, but not on a workbook inside a GUI.

When I use the same method in my GUI, the same example array "A" does
not appear on the spreadsheet.
If A has a single value, that value will be pasted over the whole
range specified, but if numel(A)>1, nothing appears
0
Reply Adam 7/21/2010 8:15:44 AM


On Jul 21, 9:15=A0am, Adam Chapman <adamchapman1...@hotmail.co.uk>
wrote:
> On Jul 21, 8:57=A0am, Adam Chapman <adamchapman1...@hotmail.co.uk>
> wrote:
>
>
>
>
>
> > I've managed to select a range of cells in my workbook and I want to
> > paste an array into those cells.
>
> > Select(Range(ActSheet,'A1:C100'));
>
> > I then tried to use
> > ActCell =3D get(Spreadsheet1,'ActiveCell');
> > set(ActCell,'Value',data);
>
> > but it didn't work. I assumed the active cell property would cover the
> > whole range selected, but it doesn't appear so.
>
> > If I try
> > ActRange =3D get(Spreadsheet1,'Range');
> > I recieve an error:
>
> > ??? Invoke Error: Incorrect number of arguments
>
> > Error in =3D=3D> torquecurveinput8>labelupdate at 264
> > =A0 =A0 ActRange =3D get(Spreadsheet1,'Range');
>
> > How can I successfully paste an array of data without having to run
> > through each individual cell in the range with nested for loops?
>
> Lines 21-23 of the example athttp://www.mathworks.com/support/solutions/e=
n/data/1-17PWC/index.html...
> work, but not on a workbook inside a GUI.
>
> When I use the same method in my GUI, the same example array "A" does
> not appear on the spreadsheet.
> If A has a single value, that value will be pasted over the whole
> range specified, but if numel(A)>1, nothing appears- Hide quoted text -
>
> - Show quoted text -


This is my method:
%make handle to current sheet
Activesheet =3D Spreadsheet1.Activesheet;
% Put a MATLAB array into Excel
A =3D [1 2; 3 4];
%select region
ActivesheetRange =3D get(Activesheet,'Range','A1:B2');
%give value to handle
set(ActivesheetRange, 'Value', A);

Matlab does not complain about the above at all, although the values
don't actually appear in the spreadsheet. UNLESS I change "A" so that
it has a single value (i.e. numel(A)=3D1)


Now If I try to see if the values went in but just aren't visible,
with:

Range =3D get(Activesheet, 'Range', 'A1:B2')
B =3D Range.value

I get:
B =3D

    [NaN]    [NaN]
    [NaN]    [NaN]

0
Reply adamchapman1985 (131) 7/21/2010 8:41:40 AM

On Jul 21, 9:41=A0am, Adam Chapman <adamchapman1...@hotmail.co.uk>
wrote:
> On Jul 21, 9:15=A0am, Adam Chapman <adamchapman1...@hotmail.co.uk>
> wrote:
>
>
>
>
>
> > On Jul 21, 8:57=A0am, Adam Chapman <adamchapman1...@hotmail.co.uk>
> > wrote:
>
> > > I've managed to select a range of cells in my workbook and I want to
> > > paste an array into those cells.
>
> > > Select(Range(ActSheet,'A1:C100'));
>
> > > I then tried to use
> > > ActCell =3D get(Spreadsheet1,'ActiveCell');
> > > set(ActCell,'Value',data);
>
> > > but it didn't work. I assumed the active cell property would cover th=
e
> > > whole range selected, but it doesn't appear so.
>
> > > If I try
> > > ActRange =3D get(Spreadsheet1,'Range');
> > > I recieve an error:
>
> > > ??? Invoke Error: Incorrect number of arguments
>
> > > Error in =3D=3D> torquecurveinput8>labelupdate at 264
> > > =A0 =A0 ActRange =3D get(Spreadsheet1,'Range');
>
> > > How can I successfully paste an array of data without having to run
> > > through each individual cell in the range with nested for loops?
>
> > Lines 21-23 of the example athttp://www.mathworks.com/support/solutions=
/en/data/1-17PWC/index.html...
> > work, but not on a workbook inside a GUI.
>
> > When I use the same method in my GUI, the same example array "A" does
> > not appear on the spreadsheet.
> > If A has a single value, that value will be pasted over the whole
> > range specified, but if numel(A)>1, nothing appears- Hide quoted text -
>
> > - Show quoted text -
>
> This is my method:
> %make handle to current sheet
> Activesheet =3D Spreadsheet1.Activesheet;
> % Put a MATLAB array into Excel
> A =3D [1 2; 3 4];
> %select region
> ActivesheetRange =3D get(Activesheet,'Range','A1:B2');
> %give value to handle
> set(ActivesheetRange, 'Value', A);
>
> Matlab does not complain about the above at all, although the values
> don't actually appear in the spreadsheet. UNLESS I change "A" so that
> it has a single value (i.e. numel(A)=3D1)
>
> Now If I try to see if the values went in but just aren't visible,
> with:
>
> Range =3D get(Activesheet, 'Range', 'A1:B2')
> B =3D Range.value
>
> I get:
> B =3D
>
> =A0 =A0 [NaN] =A0 =A0[NaN]
> =A0 =A0 [NaN] =A0 =A0[NaN]- Hide quoted text -
>
> - Show quoted text -

Ah I've found it.

For anyone looking this up, "A" needs to be a cell array, i.e.
A=3D{1,2;3,4};
instead of the standard
A=3D[1,2;3,4];

:)
0
Reply adamchapman1985 (131) 7/21/2010 8:53:24 AM

3 Replies
451 Views

(page loaded in 0.086 seconds)

Similiar Articles:













7/22/2012 1:15:26 PM


Reply: