Asked by Reema Alhassan
on 13 Jul 2018 at 14:37

Hello everybody ,

I did some calculation in a loop and this is the output I need to put the value after each iteration in Nx1 matrix how can I do this?

sumOfValues1 =

'787226.437500'

sumOfValues1 =

'162571.843750'

sumOfValues1 =

'257137.468750'

sumOfValues1 =

'366862.625000'

Answer by Adam Danz
on 13 Jul 2018 at 15:12

Edited by Adam Danz
on 13 Jul 2018 at 15:15

Accepted Answer

Follow this example for n=6 loops where I store your sumOfValues1 in a n-by-1 vector. **Also, take some time to read through this document so you understand how indexing works**.

% initialize variable n = 6; a = zeros(n,1);

% Loop through and store result for i = 1:n a(i) = sumOfValues1; end

Adam Danz
on 13 Jul 2018 at 22:58

Reema, I think you're not understanding something that's critical to understand.

**1.2e+3 is the same as 1200.**

It's **the same exact number** and matlab treats both forms exactly the same.

- 1.2e+2 is the same as 120.
- 1.2e+4 is the same as 12000.
- 1.2e+5 is the same as 120000.
- 1.2e+6 is the same as 1200000.
- 1.23456789e+6 is the same as 1234567.89

You can even test that in matlab

1.23456789e+6 == 1234567.89 ans = logical 1

This is exactly why your code WORKS when you remove the sprintf() line.

To further convince you,

a = 1.23456789e+6; %1234567.89; round(a) %1234568 (rounded)

Stephen Cobeldick
on 13 Jul 2018 at 23:34

*"1.234565e+05 I used sprintf() before to make it in this format..."*

Then you have a char vector or a string, not a numeric. You need to learn about the basic data types:

https://www.mathworks.com/help/matlab/data-types_data-types.html

Reema Alhassan
on 14 Jul 2018 at 6:47

Thank you so much both of you.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.