How to calculate a printed arithmetic

1 view (last 30 days)

for A=1:3
if A==1
fprintf('2')
elseif A==2
fprintf('+')
elseif A==3
fprintf('3')
end
end
I want represent numbers and operations with code numbers. But when I run this code I get an output
2+3>>
but it doesnt give me the solution. I want to get an output which gives me 5. Please help

Accepted Answer

Chunru
Chunru on 31 Jul 2021
str = [];
for A=1:3
if A==1
str = [str sprintf('2')];
elseif A==2
str = [str sprintf('+')];
elseif A==3
str = [str sprintf('3')];
end
end
str
str = '2+3'
eval(str)
ans = 5
  2 Comments
Ernest Adamtey
Ernest Adamtey on 31 Jul 2021
It worked thank you very much.
Chunru
Chunru on 1 Aug 2021
If it worked, please consider to accept the answer.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown 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!