Data representation of Integer values in .mat file

Hello ,
I have attached mat file which has one table with x,y,polylinetype ..etc
In Table "y" , i could see the row number 9 has whole number i.e instead of 3787 on 3.7870e+3 but rest of the data are represented as 1.3e+3
how can i represent all the data in syn form i.e 1.23e+3 ?

 Accepted Answer

Use the format command with any of the options
  • short
  • long
  • shorte
  • longe
All of these will represent the values that happen to be exact integers, in the same form as the other numbers.
However, none of these will be similar to 1.23e+3 with one leading decimal place, then the '.', then two digits, then exponent. If you want something like that, you need to do the equivalent of
fprintf('%.2e\n',matfile.y)
By the way, you can see by
>> num2hex(matfile.y(8:10))
ans =
3×8 char array
'456ceeaf'
'456cb000'
'456c5502'
that y(9), the middle of those lines, is being represented by the same data format as the other numbers.

More Answers (1)

Use the format operator found here
>> format shortEng
>> 3787
ans =
3.7870e+003

7 Comments

How can i generatlize this for complete set of data for the attached matfile i have provided before ?
also , how can i check whether my set of aray has not fullfilled the format ?
How can i generatlize this for complete set of data for the attached matfile i have provided before ?
You cannot do that easily. Your struct includes multiple data types, including graphic objects, and you have limited control over how matlab displays non-numeric fields.
@Walter : thats the problem, how can i do that , can you help me with the code ?
I do not think I understand what you are asking for?
matfile =
struct with fields:
x: [71×1 single]
y: [71×1 single]
PolylineType: {'Guardrail(motorway)'}
subLabelType: {'Mono'}
Color: ' ([85 96 112]./255)'
PolylineNumber: 10
MATLAB's default display of struct never displays the numeric content of a numeric field that has more than one row.
MATLAB's default display of struct that has a numeric row, examines to see how wide the current screen is, and uses the current "format" to figure out how many items fit across the display, and displays at most that many, using the [1 x WHATEVER] notation if there are more items. For example
>> format shortg
>> foo
foo =
struct with fields:
y: [0.24914 0.37512 0.41353 0.16518 0.94197]
>> format long g
>> foo
foo =
struct with fields:
y: [1×5 double]
MATLAB does not provide any way to customize the display of struct fields. It is not possible to tell MATLAB to use %.2e format for example; the only control is the format command.
If you were displaying the content of matfile.y by itself then the best way would be to use fprintf() with whatever format you want, such as the fprintf('%.2e\n',matfile.y) that I showed.
it has resolved my doubt but again last question on the same topic.
If i use fprintf('%.2e\n',matfile.y) to write on json file, i see there is a data loss ?
So in this case i can take %.10e to avoid data loss right ?
You need %.16e to avoid data loss
Many many Thanks Walter Roberson :-)

Sign in to comment.

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!