From: Heman on
> Sorry this is a bit of a pain. Firstly don't use %x use %s since you want to write the hex string and not the numerical representation of the string
>
> put your hex data in a cell array
>
> a = cellfun(@dec2hex,num2cell(1:200),'uniformoutput',false)
>
> fid = fopen('test.txt','wt')
> fprintf(fid,'%s\n',a{:})
> fclose(fid)
>
> replace 1:200 with your decimal data matrix

I have a small prob. i cant do this directly as my input data is in float. So i have written this float data to convert into binary. and i have a code which converts binary to hex. But now the problem is when i give the float data from the text file it reads and converts the binary and when i send it to the bin2hex function it gives me the error:

??? Error using ==> bin2hex at 29
Input must be a string or character or cell array.

Error in ==> test_bin2hex at 20
hex=bin2hex(out);


Here is the code i have written:

fid = fopen('in_data.txt', 'r');
var = fscanf(fid, '%f');
fclose(fid);
out(1:length(var))=0;
fid = fopen ('out_data_bin.txt','wt');
for i=1:length(var)
out=decimal2binary(var(i));
hex=bin2hex(out);
fprintf(fid,'%s \n',hex);
end
fclose (fid);
From: someone on
"Heman " <kokahemant(a)hotmail.com> wrote in message <i24lsm$jga$1(a)fred.mathworks.com>...
> > Sorry this is a bit of a pain. Firstly don't use %x use %s since you want to write the hex string and not the numerical representation of the string
> >
> > put your hex data in a cell array
> >
> > a = cellfun(@dec2hex,num2cell(1:200),'uniformoutput',false)
> >
> > fid = fopen('test.txt','wt')
> > fprintf(fid,'%s\n',a{:})
> > fclose(fid)
> >
> > replace 1:200 with your decimal data matrix
>
> I have a small prob. i cant do this directly as my input data is in float. So i have written this float data to convert into binary. and i have a code which converts binary to hex. But now the problem is when i give the float data from the text file it reads and converts the binary and when i send it to the bin2hex function it gives me the error:
>
> ??? Error using ==> bin2hex at 29
> Input must be a string or character or cell array.
>
> Error in ==> test_bin2hex at 20
> hex=bin2hex(out);
>
>
> Here is the code i have written:
>
> fid = fopen('in_data.txt', 'r');
> var = fscanf(fid, '%f');
> fclose(fid);
> out(1:length(var))=0;
> fid = fopen ('out_data_bin.txt','wt');
> for i=1:length(var)
> out=decimal2binary(var(i));
> hex=bin2hex(out);
> fprintf(fid,'%s \n',hex);
> end
> fclose (fid);

What does decimal2binary do that dec2bin doesn't?

The "builtin" function dec2bin returns a string that bin2hex seems to want.

If out is a "binary integer", then maybe you can get away with:
> hex=bin2hex(int2str(out));
Or can you just modify decimal2binary to output a string.
From: Walter Roberson on
Heman wrote:

> I have a small prob. i cant do this directly as my input data is in
> float. So i have written this float data to convert into binary. and i
> have a code which converts binary to hex. But now the problem is when i
> give the float data from the text file it reads and converts the binary
> and when i send it to the bin2hex function it gives me the error:

Any particular reason not to use num2hex() as I suggested earlier?
From: Heman on
Walter Roberson <roberson(a)hushmail.com> wrote in message <i24qig$len$2(a)canopus.cc.umanitoba.ca>...
> Heman wrote:
>
> > I have a small prob. i cant do this directly as my input data is in
> > float. So i have written this float data to convert into binary. and i
> > have a code which converts binary to hex. But now the problem is when i
> > give the float data from the text file it reads and converts the binary
> > and when i send it to the bin2hex function it gives me the error:
>
> Any particular reason not to use num2hex() as I suggested earlier?

Hi,

yea the reason for not using num2hex is i am dealing with fractional numbers i.e 0.5, 0.325 and so on. which needs to be handled differently as compared to regular integer numbers.

Hey Someone,

your input of the int2str worked. This thing resolved the problem.

Thanks guys!!!
From: us on
"Heman " <kokahemant(a)hotmail.com> wrote in message <i29urk$bp9$1(a)fred.mathworks.com>...
> Walter Roberson <roberson(a)hushmail.com> wrote in message <i24qig$len$2(a)canopus.cc.umanitoba.ca>...
> > Heman wrote:
> >
> > > I have a small prob. i cant do this directly as my input data is in
> > > float. So i have written this float data to convert into binary. and i
> > > have a code which converts binary to hex. But now the problem is when i
> > > give the float data from the text file it reads and converts the binary
> > > and when i send it to the bin2hex function it gives me the error:
> >
> > Any particular reason not to use num2hex() as I suggested earlier?
>
> Hi,
>
> yea the reason for not using num2hex is i am dealing with fractional numbers i.e 0.5, 0.325 and so on. which needs to be handled differently as compared to regular integer numbers.

???
are you sure you don't mix up NUM2HEX with DEC2HEX(?)...

vh=num2hex([.5,.25,.125,pi,inf,nan])
vd=hex2num(vh)
%{
% vh =
3fe0000000000000
3fd0000000000000
3fc0000000000000
400921fb54442d18
7ff0000000000000
fff8000000000000
% vd =

0.5
0.25
0.125
3.1416
Inf
NaN
%}

us
First  |  Prev  | 
Pages: 1 2
Prev: plotting 3d 'cone'
Next: Speed bottleneck