Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: Java String[] conversion

Tags:

java

matlab

I'm quite new to MATLAB programming and I ran into some trouble:

I want to call a dSPACE MLIB libriary function. According to their samples, it requires a string array as argument:

variables = {'Model Root/Spring-Mass-Damper System/Out1';...
         'Model Root/Signal\nGenerator/Out1'};

libFunction(variables);

This variables is passed to the function. My problem is now: I have a frontend application where the user can choose from an arbitary number of strings which should be passed to the matlab function. Since the frontend is writtten in Java, the type of the incoming data is java.lang.String[]. How can I convert an array of java strings to something with the same type as the sample variable above (I think it is a cell array of cell arrays or sth like that).

Thanks in advance!

like image 521
Oromis Avatar asked May 08 '12 06:05

Oromis


1 Answers

Take a look at the documentation. MATLAB makes it very easy to convert to and from Java types.

  1. Handling data returned from Java
  2. Dealing with Java arrays

You can convert an array of Java strings to either a cell or char array in MATLAB. Using cell arrays can work even with jagged arrays (which are permitted in Java).

Here are two simple examples:

%# Preparing a java.lang.String[] to play with.
a = javaArray('java.lang.String',10);
b = {'I','am','the','very','model','of','a','modern','major','general'};
for i=1:10; a(i) = java.lang.String(b{i}); end;

%# To cell array of strings. Simple, eh?
c = cell(a);

%# To char array. Also simple.
c = char(a);
like image 79
reve_etrange Avatar answered Oct 15 '22 21:10

reve_etrange