Quantcast
Channel: MATLAB Central Newsreader - converting int to float
Viewing all articles
Browse latest Browse all 7

Re: converting int to float

$
0
0
grantb@sysmatrix.net (Brett) writes:

> I guess I need to rephrase the question. With the fread command, if I
> tell it to read in an int8, it reads 8 bits and stores it in matlab as
> a double. But to create what fread would read when given the double
> option would require 64 bits. Is there an exisiting function to
> combine 4 8-bit words and interpret them as a double?
>
> If I understand it correctly, the conversion of matlab type double to
> say matlab type uint8 is simply to save memory, which is not what I am
> after.

If you're on a Windows platform, use WinU8 from matlabcentral. For
now, the URL is

http://www.mathworks.com/matlabcentral/fileexchange/Files.jsp?type=category&id=&fileId=352

but as you can see, the name is not in here anywhere, which means the
ID's might change to make the URL useless. Hurray MW! FTP was
better.

If you're on UNIX, try a MEX file like this (works for me):


#include "mex.h"

void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
  int N;
  double *outp, *inp;

  if(nrhs < 1)
    mexErrMsgTxt("One input req'd");
  if(!mxIsUint8(prhs[0]))
    mexErrMsgTxt("Input must be uint8");

  N = mxGetNumberOfElements(prhs[0]);
  if(N % 8)
    mexErrMsgTxt("Input must have multiple of 8 elements");

  plhs[0] = mxCreateDoubleMatrix(N/8, 1, mxREAL);

  outp = mxGetPr(plhs[0]);
  inp = mxGetPr(prhs[0]);
  memcpy(outp, inp, N);
}


--
Peter Boettcher
MIT Lincoln Laboratory
boettcher@ll.mit.edu
(781) 981-5275

Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles



Latest Images