MATLAB Randomly Changes Values in Array to Scientific Notation

I have a script that reads inputs from an excel file, then stores specific arrays that I need, as different variables.
These will later need to be written to an output txt file along with other strings (this output needs to be a specific format so it can feed an executable).
My problem is that when I read the data from the excel file, MATLAB keeps changing only two of the values to scientific notation.
My code is structured like so:
DATAINEED = readtable(fullfile(input_location,filename));
array1 = unique(DATAINEED.array1);
array2 = unique(DATAINEED.array2);
My problem is MATLAB then stores "array1" like so when I open the variable:
1000
2000
3000
4e+03
5000
6e+03
If I type "array1" in the command window, it prints out just fine (1000, 2000, 3000, 4000...etc.)
The problem is, if I try to print the variable along with another string so it can feed into the text file, it keeps that weird scientific notation for the couple of values.
I'm using the following code:
OutputRow1 = "array1 has: "+sprintf('%d ',array1');
This results in:
OutputRow1 =
"array1 has: 1000 2000 3000 4e+03 5000 6e+03"
Array 2 is completely fine (also an array of numbers, but not in the thousands). I've tried format short g and long g, doesn't help. Wondering if anyone has any insights, suggestions or other ways to accomplish what I need.

2 Commenti

Please upload the original Excel file by clicking the paperclip button.
"Wondering if anyone has any insights, suggestions or other ways to accomplish what I need."
As Steven Lord wrote, most likely the data are not integer valued. You could round them.
Rounding helped solve the issue. There must be something weird going on with the original code that outputs the data into the excel file.

Accedi per commentare.

 Risposta accettata

It is literally a certainty that some of those values are only approximately integer. No question in my mind. And is certainly not random, though it may be arbitrary as far as you are concerned, if you are not certain how they were created in a way to deviate from integer form.
A = [1000, 2000.1, 3000, 4000+1e-12, 5000, 6000 - 1e-11]
A = 1×6
1.0e+03 * 1.0000 2.0001 3.0000 4.0000 5.0000 6.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
OutputRow1 = "array1 has: "+sprintf('%d ',A')
OutputRow1 = "array1 has: 1000 2.000100e+03 3000 4.000000e+03 5000 6.000000e+03 "
As you can see, I changed three of the values by a bit. Those that were indeed exactly integer are shown with no decimal point. 2000 I tweaked by more than the others though, and there you can see the perturbation. But for the others, you need to look harder.
Written here with more digits yet, we can see that the numbers are not even exactly as I created tham. Only the integers are exact.
num2str(A',55)
ans = 6×46 char array
' 1000' '2000.09999999999990905052982270717620849609375' ' 3000' ' 4000.0000000000009094947017729282379150390625' ' 5000' ' 5999.9999999999899955582804977893829345703125'
You can round them of course.
B = round(A)
B = 1×6
1000 2000 3000 4000 5000 6000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
And now the rounded form has been returned to true integer forms as flints, thus integers, stored in floating point form.

Più risposte (1)

You may think the values in the array are integer values, but they're not. They're just barely different from an integer value. I'll define your printing function as an anonymous function so I can use it repeatedly.
printArray = @(x) "array1 has: "+sprintf('%d ', x');
Let's take an array that we know has exact integer values. Displaying it with disp, typing the name of the variable, or using sprintf prints the values as exact integers.
exactIntegerValues = 1000:1000:6000;
disp(exactIntegerValues)
1000 2000 3000 4000 5000 6000
exactIntegerValues
exactIntegerValues = 1×6
1000 2000 3000 4000 5000 6000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
printArray(exactIntegerValues)
ans = "array1 has: 1000 2000 3000 4000 5000 6000 "
Let's tweak one of the values so it's almost, but not quite, exactly an integer.
notQuiteInteger = exactIntegerValues;
notQuiteInteger(4) = notQuiteInteger(4) + 1e-8;
We can show element 4 is not exactly an integer.
isElement4AnInteger = isequal(notQuiteInteger(4), round(notQuiteInteger(4)))
isElement4AnInteger = logical
0
If I display it with disp or by typing the name of the variable, it kind of looks like integer values, particularly if you're using a different display format like 'shortg'.
disp(notQuiteInteger)
1.0e+03 * 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000
notQuiteInteger
notQuiteInteger = 1×6
1.0e+03 * 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
format shortg
disp(notQuiteInteger)
1000 2000 3000 4000 5000 6000
But because that fourth element is not exactly an integer value, sprintf can't print it using %d. So as stated in the description of the formatSpec input argument for sprintf on its documentation page, "If you specify a value that does not fit the conversion, such as a non-integer numeric value for a text conversion, MATLAB® may override the specified conversion." I believe in this case it's using '%e' instead.
printArray(notQuiteInteger)
ans = "array1 has: 1000 2000 3000 4.000000e+03 5000 6000 "
sprintf('%e', notQuiteInteger(4))
ans = '4.000000e+03'

Categorie

Richiesto:

il 11 Giu 2026 alle 15:20

Commentato:

circa 5 ore fa

Community Treasure Hunt

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

Start Hunting!

Translated by