ASCII String with SCI Transmit
14 次查看(过去 30 天)
显示 更早的评论
Anyone here know how to do a number to string conversion, matlab has a num2str() which would be perfect but it is not compatible with embedded matlab fcn.
My target is a TI chip (F28335). My goal is to send measurement data and see it in hyperterminal but i need to convert the numbers to ASCII before i transfer it out or i will get garbage, which i have been getting.
2 个评论
Walter Roberson
2011-2-22
None of fprintf(), sprintf(), num2str() are in the Embedded Matlab subset,
http://www.mathworks.com/help/toolbox/eml/ug/bq1h2z7-9.html
采纳的回答
Daniel
2011-2-24
1 个评论
Walter Roberson
2011-2-24
Sorry, I can't answer that with any authority: I'm going by what I've been able to _find_ in the documentation, but I don't have the appropriate toolkits to experiment with.
更多回答(5 个)
Walter Roberson
2011-2-22
Daniel, it would help to know if there are constraints on the class or range of numbers to be converted. For example is this unsigned integers or is it full floating point in exponential notation (and if so how many digits are you looking for) ?
1 个评论
Walter Roberson
2011-2-22
For simple positive integers:
n = 3141;
if n == 0
s = uint8(48)
else
s = zeros(1,ceil(log10(n+1)),'uint8');
for t = size(s,2):-1:1
s(t) = mod(n,10) + 48;
n = floor(n./10);
end
end
%s is now the vector of uint8
Daniel
2011-2-22
3 个评论
Walter Roberson
2011-2-22
To confirm: you have a measurement that might be represented by any of the arithmetic types, and you would like to convert it to an ASCII representation using an arbitrary formatting including possibilities such as '%+09f.2' ?
Or are you saying that you only need to represent integral values and the data type could be whatever is most convenient for programming purposes ?
Doug Eastman
2011-2-23
Have you tried the xPC Target block? As far as I know it is not specific to the xPC kernel and could work on the TI processor.
Daniel
2011-2-22
1 个评论
Walter Roberson
2011-2-22
What you are asking for is not simple. glibc went through a lot of trouble to get the conversions right with proper rounding (consider, for example, denormalized numbers.)
I suggest that you consider using the vprintf() source from glibc, provided that the license terms are compatible with your purposes.
Can simulink embedded package do a simple number to string conversion without s-function? I don't know, but the embedded function list I referenced above is suggestive that it does not, as otherwise int2str() would probably be supported at a minimum.
vprintf() is a relatively large routine that would often take up too much memory to be worth-while.
It is not uncommon for programmers to find that they can live with (say) fixed point, two decimal places -- something easily done by round(x*100), convert to string, insert the '.' character at the proper location.
Daniel
2011-2-23
2 个评论
Walter Roberson
2011-2-23
I've used such things too, but they didn't have floating point conversions in the ones I had.
Walter Roberson
2011-2-23
One of the vfprintf source files is at
http://www.opensource.apple.com/source/Libc/Libc-166/stdio.subproj/vfprintf.c
You would not be able to use this directly, as it needs stdarg for the arguments; you would need a layer that converted Matlab variable argument lists to stdarg . I do not know, however, whether embedded Matlab supports variable arguments.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!