From: Jose on
Hello Guys, somebody can explain when I have to use uint32, single or int16, see code below...thanks in advance:


function [sdata]=makesdata;

global sdata

%function [sdata]=makesdata;
%creates a dummy structure sdata


sdata.ptsperchan=0;
sdata.usperadc=single(0);
sdata.stimtype=0;
sdata.datasize=0;
sdata.adcdata=[];




data =

Columns 1 through 34

4 36 64 0 3 0 0 0 74 36 64 0 3 0 0 0 32 0 0 0 15 0 0 0 204 42 33 1 0 0 0 0 104 249

Columns 35 through 44

18 0 39 39 64 0 204 42 33 1


How can iIunderstand the below code? why the use, uint 32, single and int16?

sdata.ptsperchan=typecast(data(5:8),'uint32');
sdata.usperadc=typecast(data(9:12),'single');
sdata.stimtype=typecast(data(37:40),'uint32');
sdata.datasize=typecast(data(41:44),'uint32');
sdata.adcdata=typecast(adc,'int16');
From: Frédérick Cyr on
"Jose " <jose.l.vega(a)gmail.com> wrote in message <i2cb57$su7$1(a)fred.mathworks.com>...
> Hello Guys, somebody can explain when I have to use uint32, single or int16, see code below...thanks in advance:

You can use uint32, single or int16 when you want to save memory since these datatype use respectively 32 bits, 32 bits and 16 bits instead of the usual 64 bits used by MATLAB doubles. However, you must consider that these differents data types can't hold as much information as the usual matlab doubles: uint32 is restricted to unsigned integers from 0 to (2^32 -1), single is a real number with less precision than a double and int16 is restricted to signed integers from -128 to 127.

I cannot say for sure when you MUST use it with MATLAB, but my guess is "try use the datatype that was already used for your application". By this, I mean, if you see that a structure is being filled with 32 bits unsigned integers (uint32), avoid replacing this value by a double.

> How can iIunderstand the below code? why the use, uint 32, single and int16?
>
> sdata.ptsperchan=typecast(data(5:8),'uint32');
> sdata.usperadc=typecast(data(9:12),'single');
> sdata.stimtype=typecast(data(37:40),'uint32');
> sdata.datasize=typecast(data(41:44),'uint32');
> sdata.adcdata=typecast(adc,'int16');

typecast function change a value into the specified data type (see help typecast). The instructions that you see here takes some data, convert it to uint32 or single and puts it in sdata structure field.
From: Frédérick Cyr on
"Frédérick Cyr" <phraide(a)videotron.ca> wrote in message <i2ccc9$j5k$1(a)fred.mathworks.com>...

> You can use uint32, single or int16 when you want to save memory since these datatype use respectively 32 bits, 32 bits and 16 bits instead of the usual 64 bits used by MATLAB doubles. However, you must consider that these differents data types can't hold as much information as the usual matlab doubles: uint32 is restricted to unsigned integers from 0 to (2^32 -1), single is a real number with less precision than a double and int16 is restricted to signed integers from -128 to 127.

My bad, int16 are restricted to signed integers from -32768 to 32767

int8 are restricted from -128 to 127!
From: John D'Errico on
"Jose " <jose.l.vega(a)gmail.com> wrote in message <i2cb57$su7$1(a)fred.mathworks.com>...
> Hello Guys, somebody can explain when I have to use uint32, single or int16, see code below...thanks in advance:
>
>
> function [sdata]=makesdata;
>
> global sdata
>
> %function [sdata]=makesdata;
> %creates a dummy structure sdata
>
>
> sdata.ptsperchan=0;
> sdata.usperadc=single(0);
> sdata.stimtype=0;
> sdata.datasize=0;
> sdata.adcdata=[];
>
>
>
>
> data =
>
> Columns 1 through 34
>
> 4 36 64 0 3 0 0 0 74 36 64 0 3 0 0 0 32 0 0 0 15 0 0 0 204 42 33 1 0 0 0 0 104 249
>
> Columns 35 through 44
>
> 18 0 39 39 64 0 204 42 33 1
>
>
> How can iIunderstand the below code? why the use, uint 32, single and int16?
>
> sdata.ptsperchan=typecast(data(5:8),'uint32');
> sdata.usperadc=typecast(data(9:12),'single');
> sdata.stimtype=typecast(data(37:40),'uint32');
> sdata.datasize=typecast(data(41:44),'uint32');
> sdata.adcdata=typecast(adc,'int16');

Because these various types of variable can handle numbers
of various magnitudes, for various goals.

For example, if your numbers will always be integers
in the range [0,255], then you can save memory by
storing them as uint8, since that takes no more than
1 byte to store. If they are known to fall in the interval
[0,65535], then uint16 is an option.

If you can stand the loss of precision by storing a floating
point number as a single rather than the default double,
then single may be for you.

The fact is, unless you are sure that you need the savings
in memory at some cost, then just leave your numbers
alone. Don't use these types unless you have a reason
to do so, as they can cause problems unless you take
care. If someone has given you code that needs a specific
class, then as the documentation specifies.

If your goal is to save cpu cycles, converting to uint8
rarely saves time, as matlab will probably do its arithmetic
as quickly with double arrays as fast as it does integers.
And the double class will represent all integers as large
as 2^53-1 exactly anyway.

John
From: Jose on
Fred, but I can´t understand why my bosss in his code use 'single' instead of 'uint32'..in this line
sdata.usperadc=typecast(data(9:12),'single');...and I want to understand it.



"Jose " <jose.l.vega(a)gmail.com> wrote in message <i2cb57$su7$1(a)fred.mathworks.com>...
> Hello Guys, somebody can explain when I have to use uint32, single or int16, see code below...thanks in advance:
>
>
> function [sdata]=makesdata;
>
> global sdata
>
> %function [sdata]=makesdata;
> %creates a dummy structure sdata
>
>
> sdata.ptsperchan=0;
> sdata.usperadc=single(0);
> sdata.stimtype=0;
> sdata.datasize=0;
> sdata.adcdata=[];
>
>
>
>
> data =
>
> Columns 1 through 34
>
> 4 36 64 0 3 0 0 0 74 36 64 0 3 0 0 0 32 0 0 0 15 0 0 0 204 42 33 1 0 0 0 0 104 249
>
> Columns 35 through 44
>
> 18 0 39 39 64 0 204 42 33 1
>
>
> How can iIunderstand the below code? why the use, uint 32, single and int16?
>
> sdata.ptsperchan=typecast(data(5:8),'uint32');
> sdata.usperadc=typecast(data(9:12),'single');
> sdata.stimtype=typecast(data(37:40),'uint32');
> sdata.datasize=typecast(data(41:44),'uint32');
> sdata.adcdata=typecast(adc,'int16');