I want to use xlsread function in matlab to import very small data like 10E-13. But it always shows 0 in vector 'num'. I want to read the exact number and export it.
So, does anyone know how to increase the accuracy or precision?
Thank you in advance!
You can't change the precision with which xlsread reads the data. However, the output array might actually contain the data in num, but MATLAB displays it as 0. Run format long g, then diplay it again.
Related
Currently looking at this API page, I have tried inputting format loose and format compact, but to no avail. What I need to do is change the way this number is displayed on the command window:
I obtain the value by rounding it to the three most significant figures in the function which I call from my main function.
stat = round(mean(v_stat),3,'significant');
I display the values through this statement:
fprintf('Ratio of Compression for Blind Deconvolution: %d \n',stat1);
I need to know how to display this value as as it's proper state instead of being multiplied by e raised to power of something.
You need to change your format statement, e.g. (a float with 3 decimals).
fprintf('Ratio of Compression for Blind Deconvolution: %.3f \n',stat1);
see the matlab help for fprintf to understand more on the format api.
Note: the %d your using is for integers
I wrote a function that calculates the mean price of the last 15 mins of the trading day. In the attached excel file I get a mean of 55.23 but my function in MATLAB returns 55.32. I've been messing with this all day, and cannot figure out the answer for the difference. Can anyone tell me why the means are different in excel and MATLAB? Thank you.
function last15MinsOfDay=last15MinsOfDay(time,price)
% last15MinsOfDay takes the average of prices between 3:45 and 4:00.
timeStr=cellstr(datestr(time));
timeDbl=datevec(timeStr);
times=and(timeDbl(:,4)==14,timeDbl(:,5)>=46)+and(timeDbl(:,4)==15,timeDbl(:,5)==0);
priceIdx=find(times);
z=find(fwdshift(1,priceIdx)~=priceIdx+1);
z=[1; z];
mu=zeros(length(z),1);
for i = 1:length(z)-1;
mu(i)=mean(price(priceIdx(z(i):z(i+1))));
end
last15MinsOfDay=mu;
Excel file
The correct mean is 55.236 in your data. I'm not sure exactly what you're intending in the lines after the priceIdx line, but in MATLAB simply:
mean(price(priceIdx))
where priceIdx is a vector of the indices of the items you want to include should be what you need.
I'm trying to deal with an Excel file which comprises 1 million rows. However, when I open it in MATLAB, only 10,000 rows are displayed.... Could anyone tell me how to import full data using MATLAB?
Type at your MATLAB Command Window:
[~,~,data] = xlsread('X:\path to your file\excel file.xls');
where you should replace the xlsread argument with something that's suitable. The result will be a cell array of mixed numeric (if they could be converted) and strings read from the first sheet in the file. First cell in data corresponds to the upper/left cell in the worksheet.
If you want to specify the sheet, or for more refined function call, read the function's help.
NB
Some of the ways to call the xlsread function are possible or not depending on whether you have Office installed or not.
What is the difference between hist and imhist functions in Matlab? I have a matrix of color levels values loaded from image with imread and need to count entropy value of the image using histogram.
When using imhist the resulting matrix contains zeros in all places except the last one (lower-right) which contains some high value number (few thousands or so).
Because that output seems to be wrong, I have tried to use hist instead of imhist and the resulting values are much better, the matrix is fulfilled with correct-looking values instead of zeros.
However, according to the docs, imhist should be better in this case and hist should give weird results..
Unfortunately I am not good at Matlab, so I can not provide you with better problem description. I can add some other information in the future, though.
So I will try to better explain my problem..I have an image, for which I should count entropy and few other values (how much bytes it will take to save that image,..). I wrote this function and it works pretty well
function [entropy, bytes_image, bytes_coding] = entropy_single_pixels(im)
im = double(im);
histg = hist(im);
histg(histg==0) = [];
nzhist = histg ./ numel(im);
entropy = -sum(nzhist.*log2(nzhist));
bytes_image = (entropy*(numel(im))/8);
bytes_coding = 2*numel(unique(im));
fprintf('ENTROPY_VALUE:%s\n',num2str(entropy));
fprintf('BYTES_IMAGE:%s\n',num2str(bytes_image));
fprintf('BYTES_CODING:%s\n',num2str(bytes_coding));
end
Then I have to count the same, but I have to make "pairs" from pixels which are below each other. So I have only half the rows and the same count of columns. I need to express every unique pixel pair as a different number, so I multiplied the first one by 1000 and added the second one to it... Subsequently I need to actually apply the same function as in the first example, but that is the time, when I am getting weird numbers from the imhist function. When using hist, it seems to be OK, but I really don't think that behavior is correct, so that must be my error somewhere. I actually understand pretty good, to what I want to do, or at least I hope so, but unfortunately Matlab makes all that kind of hard for me :)
hist- compute histogram(count number of occurance of each pixel) in color image.........
imhist- compute histogram in two dimensional image.
Use im2double instead of double if you want to use imhist. The imhist function expects double or single-precision data to be in the [0,1] data range, which is why you see everything in the last bin of the histogram.
I'm currently writing a piece of code which is supposed to import text files using importdata and count the number of columns, i thought the cols() function would suffice for this, but it seems that all the imported data is stored as a double, meaning I can't perform this operation.
M=importdata('title');
numcols=cols(M.data);
Am I doing something wrong? I thought the data fromthe text file would be stored in a matrix/array?
Cols is a specific function for use in the database toolbox.
You probably just want to use the size function. EG:
size(M.data,2); %Returns number of columns
FYI
size(M.data, 1); %Returns number of rows.