add values to one value

Hi,
I've got 4 values: D=1 E=3 F=2 G=9
and I want to combine them to 1 value: Total=DEFG (in this example: 1329)
Anyone an idea how I could fix this?

 Accepted Answer

Here's one way:
D=1;
E=3;
F=2;
G=9;
x = str2num([num2str(D),num2str(E),num2str(F),num2str(G)])
There are probably more efficient ways, but I am feeling lazy. Note that upstream from where you define these variables like this, there were probably MUCH better naming conventions or other methods that would have made this much cleaner.

1 Comment

x = str2double(sprintf('%d%d%d%d', D, E, F, G));

Sign in to comment.

More Answers (2)

Roberto
Roberto on 16 Oct 2011

0 votes

That doesn't work like I wanted... I need a variable with the answer which is 1329.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!