How can i get 6 digits after comma (matlab)? - matlab

I read from text some comma seperated values.
-8.618643,41.141412
-8.639847,41.159826
...
I write script below;
get_in = zeros(lendata,2);
nums = str2num(line); % auto comma seperation.(two points)
for x=1:2
get_in(i,x)=nums(x);
end
it automatically round numbers. For example;
(first row convert to "-8.6186 , 41.1414")
How can i ignore round operation?
I want to get 6 digits after comma.
I tried "str2double" after split line with comma delimeter.
I tried import data tool
But it always rounded to 4 digits, too.

As one of the replies has already said, the values aren't actually rounded, just the displayed values (for ease of reading them). As suggested, if you just enter 'format long' into the command window that should help.
The following link might help with displaying individual values to certain decimal places though: https://uk.mathworks.com/matlabcentral/newsreader/view_thread/118222
It suggests using the sprintf function. For example sprintf(%4.6,data) would display the value of 'data' to 6 decimal places.

Related

matlab textscan gives me wrong number of lines

I have a file names inputR_revised.tsv at https://www.dropbox.com/s/vtby4027rvprhga/inputR_revised.tsv?dl=0
In matlab, I typed
fid=fopen('BMC3C/example/inputR_revised.tsv','r')
covTable = textscan(fid,['%s',repmat('%.8n',[1,20])],'HeaderLines',1);
I get covTable{1,1} of size 41699 times 1. However when I type the following at terminal
wc -l inputR_revised.tsv
I get 41677.
Why does it differ? I have used sed and cut to modify the original file to get inputR_revised.tsv. Is this the reason?
Is there a way to fix this?
%.8 is not enough if you have decimals printed with more than 8 digits. For these cases digits after the 8th decimal could be treated as a separate entry. That will make more numbers than expected. You should use a higher value for number of decimals in the scan format. For example,
fid=fopen('BMC3C/example/inputR_revised.tsv','r')
covTable = textscan(fid,['%s',repmat('%.18n',[1,20])],'HeaderLines',1);
This should give you the correct number of rows.

printmat function: Decimal and percentage

I am very new to MATLAB. I am sorry if my question is basic. I am using "printmat" function to show some matrices in the command console. For example, printmat(A) and printmat(B), where A = 2.79 and B = 0.45e-7 is a scalar (for the sake of simplicity).
How do I increase the precision arbitrarily to seven decimals? For example: my output looks like 2.7943234 and B = 0.00000004563432.
How do I add a currency (say dollar) figure to the output of printmat?
How do I add a percentage figure (%) to the output of printmat?
Note: The reason I use printmat is that I can name my rows and columns. If you know a better function that can do all above, I would be glad to know.
Regards Mariam. From what I understand, you would like to display the numbers and show their full precision. I am also newbie, If I may contribute, you could convert the number data to string data (for display purposes) by using the sprintf function.
I am using the variable A=2.7943234 as example. This value will not display the full precision, instead it will display 2.7943. To show all the decimal tails, you could first convert this to string by
a = sprintf('%0.8f',A);
It will set the value a to a string '2.79432340'. The %0.8f means you want it to display 8 decimal tails. For this example,%0.7f is sufficient of course.
Another example: A=0.00000004563432, use %0.14f.
A=0.00000004563432;
a=sprintf('%0.14f $ or %%',A);
the output should be : '0.00000004563432 $ or %'.
You could analyze further in https://www.mathworks.com/help/matlab/ref/sprintf.html
You could try this first. If this does not help to reach your objective, I appreciate some inputs. Thanks.
The printmat function is very obsolete now. I think table objects are its intended successor (and functions such as array2table to convert a matrix to a table of data). Tables allow you to add row and column names and format the columns in different ways. I don't think there's a way to add $ or % to each number, but you can specify the units of each column.
In general, you can also format the display precision using format. Something like this may be what you want:
format long

Remove commas and decimal places from number field

I am trying to add two zero place holders in front of a field without changing the actual values involved. The field is an order number that is being pulled from MOMs. So right now that fields' formula is {cms.ORDERNO}.
When I try '00'+{cms.ORDERNO} the field displays 001,254.00. How can I remove the decimals and comma so it displays 001254?
The usual trick is to pad with plenty of extra digits on the left and then only take the six you really want from the right. This would handle any order number ranging from 1 to 999999.
right("000000" + totext({cms.ORDERNO}, "0"), 6)
When you don't specify a format string, as you tried, it uses default settings which usually come from Windows. By the way, if I recall correctly cstr() and totext() are equivalent for the most part but totext() has more options.
You should also be able to specify "000000" as the format string to produce the left-padded zeroes. Sadly I don't have Crystal Reports installed or I'd check it out for you to be sure. If this is the case then you probably don't need a formula if you just want to use the formatting options for the field on the canvas. If you do use a formula it's still simple.
totext({cms.ORDERNO}, "000000")
You definitely want to use the Replace formula a few times for this. The formula below converts ORDERNO into string, removes any commas and trailing decimal places, then adds the two zeroes at the beginning:
`00` + REPLACE(REPLACE(CSTR({cms.ORDERNO}),".00",""),",","")
So for example, if cms.ORDERNO is 1,254.00 the output from this formula would be 001254
I know this is older, but better solutions exists and I ran across this same issue. ToText has what you need built right in.
"00" + ToText({cms.ORDERNO}, 0, "")
From the Crystal Documentation:
ToText (x, y, z)
x is a Number or Currency value to be converted into a text string; it
can be a whole or fractional value.
y is a whole number indicating the number of decimal places to carry
the value in x to (This argument is optional.).
z is a single character text string indicating the character to be
used to separate thousands in x. Default is the character specified in
your International or Regional settings control panel. (This argument
is optional.)

Display multiple values in one line

I'm new to MATLAB. I have two values x and y. Both of them contain values with unknown accuracy. The problem: How could I display them both in one row with 2 digits after comma? Like:
x<tabulation or stack of spaces>y<then goes new line>
Example
RAW data
0,324352 0,75234
1,563 3,4556
Expected output
0,34 0,75
1,56 3,45
Upd: for one value it works well
disp('x=' num2str(x,3));
Purpose is: display TWO values on one row with the new line symbol
The answer is:
disp(num2str([x y],3));
The 3 value means - max.quanity of symbols after comma including it(am I wrong? just thoughts)
Another Idea:
Somehow represent X and Y as array values and then display them.

Format Matlab data to only have digits after the decimal place

I used dlmwrite to output some data in the following form:
-1.7693255974E+00,-9.7742420654E-04, 2.1528647648E-04,-1.4866241234E+00
What I really want is the following format:
-.1769325597E+00, -.9774242065E-04, .2152864764E-04, -.1486624123E+00
A space is required before each number, followed by a sign, if the number is negative, and the number format is comma delimited, in exponential form to 10 significant digits.
Just in case Matlab is not able to write to this format (-.1769325597E+00), what is it called specifically so that I can research other means of solving my problem?
Although this feels morally wrong, one can use regular expressions to move the decimal point. This is what the function
myFormat = #(x) regexprep(sprintf('%.9e', 10*x), '(\d)\.', '\.$1');
does. The input value is multiplied by 10 prior to formatting, to account for the point being moved. Example: myFormat(-pi^7) returns -.3020293228e+04.
The above works for individual numbers. The following version is also able to format arrays, providing comma separators. The second regexprep removes the trailing comma.
myArrayFormat = #(x) regexprep(regexprep(sprintf('%.9e, ', 10*x), '(\d)\.', '\.$1'), ', $', '');
Example: myArrayFormat(1000*rand(1,5)-500) returned
-.2239749230e+03, .1797026769e+03, .1550980040e+03, -.3373882648e+03, -.3810023184e+03
For individual numbers, myArrayFormat works identically to myFormat.