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:
Strange calculation behaviour

Subject: Strange calculation behaviour

From: George34 Dampf

Date: 7 Oct, 2011 13:30:26

Message: 1 of 6

Hi,

I have this simple code:
a = u
b = cx
diff = a - b
(with u=10 and cx = 3.2422149053e+02)

but i get as result :

a = 10
b = 3.2422149053e+02
diff =
  Columns 1 through 11
   -41 -36 -40 -42 -40 -40 -39 -42 -47 -38 -43
  Columns 12 through 16
   -41 -91 -33 -38 -40

if i type it directly in the command winodw i get diff= -314.2215, which seems correct, but what is wrong with running the script?

Subject: Strange calculation behaviour

From: Matt J

Date: 7 Oct, 2011 13:47:30

Message: 2 of 6

"George34 Dampf" wrote in message <j6muti$f2k$1@newscl01ah.mathworks.com>...
>
> but i get as result :
>
> a = 10
> b = 3.2422149053e+02
> diff =
> Columns 1 through 11
> -41 -36 -40 -42 -40 -40 -39 -42 -47 -38 -43
> Columns 12 through 16
> -41 -91 -33 -38 -40
==============

Not me. I get the expected result.

> if i type it directly in the command winodw i get diff= -314.2215, which seems correct, but what is wrong with running the script?
===============


Whatever the issue is, it's not MATLAB's fault. Tell us what else is in the script and your base workspace.

Subject: Strange calculation behaviour

From: HB

Date: 7 Oct, 2011 13:53:44

Message: 3 of 6

On 7 Okt., 15:47, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> "George34 Dampf" wrote in message <j6muti$f2...@newscl01ah.mathworks.com>...
>
> > but i get as result :
>
> > a = 10
> > b = 3.2422149053e+02
> > diff =
> >   Columns 1 through 11
> >    -41   -36   -40   -42   -40   -40   -39   -42   -47   -38   -43
> >   Columns 12 through 16
> >    -41   -91   -33   -38   -40
>
> ==============
>
> Not me. I get the expected result.
>
> > if i type it directly in the command winodw i get diff= -314.2215, which seems correct, but what is wrong with running the script?
>
> ===============
>
> Whatever the issue is, it's not MATLAB's fault. Tell us what else is in the script and your base workspace.

Also, you should try NOT to bless variables with names already used my
Matlab, such as diff . Since it works in you command prompt, it may
that you're using another workspace than the expected one, and
therefore get these results.

Subject: Strange calculation behaviour

From: Matt J

Date: 7 Oct, 2011 14:09:10

Message: 4 of 6

HB <mister.hb@gmail.com> wrote in message <c0e94d94-7078-4720-9acb-72b7f8cbbdcf@z8g2000yqb.googlegroups.com>...
>
>Since it works in you command prompt, it may
> that you're using another workspace than the expected one, and
> therefore get these results.

And if that's the case, it is a function file and for the sake of clarity you should stop calling it a "script". Script is a term reserved for mfiles without a function syntax header and which don't have their own isolated workspaces.

Subject: Strange calculation behaviour

From: George34 Dampf

Date: 7 Oct, 2011 14:18:14

Message: 5 of 6

Thanks, changing variable names and restarting matlab helped.
Sorry for bothering you.

Subject: Strange calculation behaviour

From: Steven_Lord

Date: 7 Oct, 2011 15:19:38

Message: 6 of 6



"George34 Dampf" <lichtamendedessargs@googlemail.com> wrote in message
news:j6muti$f2k$1@newscl01ah.mathworks.com...
> Hi,
>
> I have this simple code:
> a = u
> b = cx
> diff = a - b
> (with u=10 and cx = 3.2422149053e+02)

What data type does the WHOS function say both cx and b are? From your
results, I'm pretty sure it's going to say "char" rather than "double". If
that's the case, your cx is NOT the number 3.2422149053e+02 but the _string_
'3.2422149053e+02' and your a-b calculation subtracts the ASCII value of the
characters '3', '.', '2', etc. from 10.

> but i get as result :
>
> a = 10
> b = 3.2422149053e+02
> diff =
> Columns 1 through 11
> -41 -36 -40 -42 -40 -40 -39 -42 -47 -38 -43
> Columns 12 through 16
> -41 -91 -33 -38 -40

double('3') [which returns the ASCII value of the character '3'] is 51.
10-51 is indeed the -41 that is the first element of your output.

> if i type it directly in the command winodw i get diff= -314.2215, which
> seems correct, but what is wrong with running the script?

You need to figure out where and why cx becomes a char array. If you see
this behavior in a standalone application compiled with MATLAB Compiler,
read:

http://blogs.mathworks.com/loren/2010/12/21/strings-and-numbers-as-arguments/

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with 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