Azzera filtri
Azzera filtri

Compiled standalone matlab executable fails at math; what am I doing wrong?

1 visualizzazione (ultimi 30 giorni)
Given file test.m:
function test( a, b )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
a
b
x = a + b;
x
fileID = fopen('test.out', 'w');
fprintf(fileID, 'x=%d', x);
fprintf('%s', 'test.out');
end
compiled with: mcc -m -R nodisplay test.m -d out/
I would expect: out/test.app/Contents/MacOS/test 1 1
To give me 2, both in test.out and on the console. It however returns:
a =
1
b =
1
x =
98
test.out
And test.out contains: x=98
I am flabbergasted... Within matlab everything works as expected. What changes during compile? And how do I fix this

Risposta accettata

Andrew Schenk
Andrew Schenk il 1 Mar 2015
When you use the MATLAB Compiler, the input argument type will strings.
When the function displays a and b, it displays the string. However when the addition is preformed, MATLAB dynamically converts the strings into numbers, but using their ASCII value. The character 1 has an ASCII value of 49. Thus 49+49 = 98.
What you should do instead is add the following to the top of your function:
a = str2num(a);
b = str2num(b);

Più risposte (1)

Tino de Bruijn
Tino de Bruijn il 2 Mar 2015
Ah, I did read that somewhere. And I also figured 98 was probably a 'b', but it was too late to make the connection though...
Thanks!

Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by