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.
採用された回答
その他の回答 (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)
exactIntegerValues
printArray(exactIntegerValues)
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)))
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)
notQuiteInteger
format shortg
disp(notQuiteInteger)
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)
sprintf('%e', notQuiteInteger(4))
カテゴリ
ヘルプ センター および File Exchange で Numeric Types についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!