Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Access changes that a Java method makes?

Subject: Access changes that a Java method makes?

From: Michael Rinderer

Date: 24 Jul, 2009 09:10:17

Message: 1 of 4

Java method:
public final void getData(int xChannel, double[] xData)

Matlab code:
xData = zeros(500,1);
scope.getData(1,xData);

The values in xData are changed by the java function. These changes are not visible in Matlab after calling the function, because Matlab passes only the values.

Is there a way to pass a pointer and not the values?

Thanks

Subject: Access changes that a Java method makes?

From: Yair Altman

Date: 24 Jul, 2009 13:53:01

Message: 2 of 4

"Michael Rinderer" <M.RINDERER@bachmann.info> wrote in message <h4btpp$jbg$1@fred.mathworks.com>...
> Java method:
> public final void getData(int xChannel, double[] xData)
>
> Matlab code:
> xData = zeros(500,1);
> scope.getData(1,xData);
>
> The values in xData are changed by the java function. These changes are not visible in Matlab after calling the function, because Matlab passes only the values.
>
> Is there a way to pass a pointer and not the values?


Instead of passing the Matlab array directly, store it in a javaArray. On the way back from the Java function, you can access the array's elements directly, or convert back to Matlab:

% Initialize jxData:
xData = zeros(500,1);
[numRows, numCols] = size(xData);
jxData = javaArray('java.lang.Double',numRows,numCols);
for rowIdx = 1 : numRows
   for colIdx = 1 : numCols
      jxData(rowIdx, colIdx) = java.lang.Double(xData(rowIdx,colIdx));
   end
end
jxData = [jxData(:)]; % [][] => [] (i.e., 2D => 1D array)

% Process/modify data
scope.getData(1,jxData);

% Convert back to Matlab
xData_new = cell2mat(cell(jxData));


Yair Altman
http://UndocumentedMatlab.com
 

Subject: Access changes that a Java method makes?

From: Michael Rinderer

Date: 27 Jul, 2009 06:58:01

Message: 3 of 4

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <h4cebt$8k7$1@fred.mathworks.com>...
> "Michael Rinderer" <M.RINDERER@bachmann.info> wrote in message <h4btpp$jbg$1@fred.mathworks.com>...
> > Java method:
> > public final void getData(int xChannel, double[] xData)
> >
> > Matlab code:
> > xData = zeros(500,1);
> > scope.getData(1,xData);
> >
> > The values in xData are changed by the java function. These changes are not visible in Matlab after calling the function, because Matlab passes only the values.
> >
> > Is there a way to pass a pointer and not the values?
>
>
> Instead of passing the Matlab array directly, store it in a javaArray. On the way back from the Java function, you can access the array's elements directly, or convert back to Matlab:
>
> % Initialize jxData:
> xData = zeros(500,1);
> [numRows, numCols] = size(xData);
> jxData = javaArray('java.lang.Double',numRows,numCols);
> for rowIdx = 1 : numRows
> for colIdx = 1 : numCols
> jxData(rowIdx, colIdx) = java.lang.Double(xData(rowIdx,colIdx));
> end
> end
> jxData = [jxData(:)]; % [][] => [] (i.e., 2D => 1D array)
>
> % Process/modify data
> scope.getData(1,jxData);
>
> % Convert back to Matlab
> xData_new = cell2mat(cell(jxData));
>
>
> Yair Altman
> http://UndocumentedMatlab.com
>

Thanks for your fast answer.
Unfortunately it doesn’t work:
When I pass the javaArray an exception is thrown:
“??? No method 'getData' with matching signature found for class
'at.bachmann.vis.dacc.proxy.m1.rec.scope.Scope'.”

It seems, that the function public final void getData(int xChannel, double[] xData) doesn’t accept the wrapper class Double[].

No error is thrown when I pass a double[] array. But then the changes aren’t visible in Matlab.

I’m using Matlab 7.5.0.

Subject: Access changes that a Java method makes?

From: Michael Rinderer

Date: 4 Aug, 2009 08:59:03

Message: 4 of 4

Workaround:
Write a java class around the method which passes the arguments.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us