sababi0314
sababi0314

Reputation: 21

MATLAB create a converter function (input: whole number) (output: string)

In MATLAB, I want to create a function that takes input a whole number and output a string without using built-in functions such as num2str, int2str, or mat2str. The whole number can possibly with a leading minus sign. Please help!

Upvotes: 0

Views: 179

Answers (1)

rayryeng
rayryeng

Reputation: 104545

What I would do is split up your number into individual digits and place them into an array. Then, use char to convert this numeric array into characters. However, what you need to do is take each number and offset by 48. The reason why is because char takes numbers stored into an array, and finds their ASCII equivalent characters mapped to these numbers and concatenates all of these characters into single string. If you consult any ASCII table, you'll see that the characters from 0-9 get mapped to 48-57.

The first problem is to figure out how to split up your number into digits. Check out my previous post on how to extract the nth digit from a number in MATLAB. This function only works with extracting one digit. In addition, this assumes that the number is positive. We want to get all of the digits, and so you simply need to vectorize its use. As such, what we will do for now is extract the digits out normally disregarding the sign of the number. We do this by applying the absolute value function to this number, then apply the algorithm normally. We can append a negative sign at the end of the algorithm should the number be negative.

With the algorithm that I wrote in the previous post, by vectorizing its use, a consequence to this is that each digit will be stored into an element in an array. Supposing your number was stored in A, we could extract out a numeric array like so:

numDigits = floor(log10(abs(A))) + 1; %// Calculate total number of digits seen in the number
n = 1 : numDigits; %// Place digits into individual elements in the array
d = numDigits - (n - 1);
out_seq = floor(mod(abs(A), 10.^d) ./ 10.^(d-1)); %// Note: Absolute value

Now the final step is to determine whether or not the number is negative. If it is, we append a - sign to the string. If not, just convert the number manually:

if A < 0
   out = ['-' char(out_seq + 48)];
else
   out = char(out_seq + 48);
end

Now, let's put this into a function to be complete:

function [out] = num_converter(A)

numDigits = floor(log10(abs(A))) + 1; %// Calculate total number of digits seen in the number
n = 1 : numDigits; %// Place digits into individual elements in the array
d = numDigits - (n - 1);
out_seq = floor(mod(abs(A), 10.^d) ./ 10.^(d-1)); %// Note: Absolute value

if A < 0
   out = ['-' char(out_seq + 48)];
else
   out = char(out_seq + 48);
end

Calling this function on a few of examples, this is what I get:

>> out = num_converter(669965)

out =

669965

>> class(out)

ans =

char

>> out = num_converter(-669965)

out =

-669965

>> class(out)

ans =

char

>> out = num_converter(-633)

out =

-633

>> class(out)

ans =

char

>> out = num_converter(63323)

out =

63323

>> class(out)

ans =

char

Also take note that I checked the class of each output when I ran the function. As verified, they're all of type char, which is a string.

Upvotes: 2

Related Questions