I am trying to load ascii files into Matlab which contain 1020 rows and two columns of spectral data. When I use dlmread like below, Matlab turns this into a matrix N, which is what I want:
N = dlmread('alummatrix.asc')
However, I want it to read only the first 80 rows of data and ignore the rest, and then do this for all .asc files in a directory.
Also, I want the decimal number not to change or get rounded. It's outputting my data 5 decimal figures to the left of the original data. Also, I'd like it to retain its original notation and not to round:
It gives me:
N =
1.0e+05 *
0.0384 0.3374
When I just want it to show up like:
N =
3838 33738
Use this line of code:
N = dlmread('alummatrix.asc','',[0 0 80 0]);
Good luck!
Related
I have a .binary file that contains Depth data from a kinect sensor.
I am trying to go through the .binary file and get back the actual image in MATLAB. So this is the MATLAB program that I came up with:
fid = fopen('E:\KinectData\March7Pics\Depth\Depth_Raw_0.binary');
col = 512; %// Change if the dimensions are not proper
row = 424;
frames = {}; %// Empty cell array - Put frames in here
numFrames = 0; %// Let's record the number of frames too
while (true) %// Until we reach the end of the file:
B = fread(fid, [col row],'ushort=>ushort'); %// Read in one frame at a time
if (isempty(B)) %// If there are no more frames, get out
break;
end
frames{end+1} = B.'; %// Transpose to make row major and place in cell array
numFrames = numFrames + 1; %// Count frame
imwrite(frames{numFrames},sprintf('Depth_%03d.png',numFrames));
end
%// Close the file
fclose(fid);
frm = frames{1};
imagesc(frm)
colormap(gray)
The above program works fine but it would not give me any image thats above 99.
That is, I would be processing the .binary file and the last image I obtained is Depth_099.png even though the full video has more than that.
Does anyone knows y?
Thanks
The reason why you're not getting the images above 99 is because of the way you are format specifying your integer as you are creating your file name string as you read in the file. Specifically, here:
imwrite(frames{numFrames},sprintf('Depth_%03d.png',numFrames));
%03d.png means that you are only specifying up to 3 digits of precision, and so 999 is the max you will get. If you surpass 999, then your characters for your file name will also expand in size, so Depth_1000.png or Depth_124141.png for example. The %03d in the formatting string ensures that your number has three digits of precision, zero-padding to the left of the number to ensure that you have that many digits. If you want to maintain the same number of characters for your file name, one fix is to probably increase the number of digits of precision, something like:
imwrite(frames{numFrames},sprintf('Depth_%05d.png',numFrames));
This way, the length of the string will be longer, and going with your convention, you'll get up to 'Depth_99999.png'. If you go beyond this, then your file names will increase in character count accordingly. If you specify %05d, you are guaranteed to have 5 digits of precision, zero-padding those numbers that have less than 5 digits accordingly.
Depending on how many frames your video contains, adjust the number accordingly.
However, given your comments below.... it could just be that you only have 99 frames of data :)... but the precision tip that I mentioned above should definitely be useful.
I am trying to build a finantial application that handle economical data using Matlab. The file I want to load is in a csv file and are double numbers in this format '1222.3'. So far, I am just working with one dimension and I am able to load the data into a vector.
The problem is that the data is loaded into the vector in String format. To change all the vector into double format I use str2double(vector), but the numbers into the vector end like this:
1222.3 -> 1.222
153.4 -> 0.1534
I have tried to multiply the vector per 100 (vector.*100), but did not work.
Any idea?
If your vector components are sufficiently large enough, MATLAB will print the numbers in exponential format.
>> a = 1234.56
a =
1.2346e+03
The numbers are also shown in scientific notation in the workspace browser:
You can print the numbers in decimal form using e.g. fprintf:
>> fprintf('%5.3f\n',a)
1234.560
>>
As a side note, 1.222 * 100 ≠ 1222 ...
Matlab automatically pulls some common factor out numerical vectors, which has confused me many times myself. The line that gives the common factor is easy to miss, especially for large vectors, because it is displayed at the top.
If I define a vector with the two number you gave, Matlab displays it to me in the following way:
It pulled out a factor of 1000, as indicated by the line 1.0e+03 *.
I'm using MATLAB to perform some statistics on some data. I have two 17x206x378 matrices where dimension 1 are subjects from the same group (so 17 subjects in matrix1, 17 in matrix 2). I want to perform ttests so I get 206 p-values. I then want to do this SEPARATELY for each of the 378 elements in the third dimension.
So say u is a 17x206x378 matrix and d is a different 17x206x378 matrix.
I basically started by doing:
[h,p,ci,s] = ttest2(u,d)
Which does in fact give me a p-matrix size 1x206x378 so everything looked great.
Then to do a quick check I just extracted the first of the third dimension elements from each matrix with:
u1=u(:,:,1); d1=d(:,:,1);
and ran test2 on this data via what you would expect:
[h1,p1,ci1,s1] = ttest2(u1,d1);
I again got a 1x206 p1-matrix of results but the values are not the same as those in the 1x206x378 p-matrix. When I plot the values in both the p(:,:,1) and the p1 vectors the resulting plots look very similar but not exactly the same.
Obviously one of these give results that are significant (below .05) in some instances where the other does not and I do not want to report a fake result so 2 questions I suppose?
1) I am under the impression I am doing the ttests on the same data so what exactly is going on here?
2) If I do ultimately want to get 206 p-values for each of the 378 third dimension elements, what is the correct way to do this?
Thanks for your help!
I ran the following code:
u = rand(17,206,378);
d = rand(17,206,378);
u1 = u(:,:,1);
d1 = d(:,:,1);
[h,p,ci,s] = ttest(u,d);
[h1,p1,ci1,s1] = ttest(u1,d1);
sum(abs(p1(1,:)- p(1,:,1)))
And the output was 0, indicating that the corresponding elements of p and p1 are the same. Maybe it's an indexing issue.
I have results which are 6 columns long however have been printed as 2 then 3 beneath then 1 beneath that! There are hundreds of lines and matlab will not except the structure of the matrix as it is now. Is there any way to tell matlab i want the first 5 results in their own columns then continuing down the rows after that?
My results appear as follows:
0.5 0
0.59095535915335684063 -0.59095535915335395405 -5.89791913085569763
33e-08
... repeated alot
thansk so much, em xx
I would just do a format shortE before you process the output, this will give you everything in scientific notation with 4 digits after the decimal. That 'should' allow you to fit your columns all in one line, so you don't have to deal with the botched output.
In general you should not want the output to be in a too specific format, but suppose you have this matrix:
M =[0.5 0 0.59095535915335684063 -0.59095535915335395405 -5.89791913085569763 33e-08];
To make it an actual matrix I will repeat it a bit:
M = repmat(M,10,1);
Now you can ensure that all six columns will fit on a normal screen by using the format.
format short
Try help format to find more options. Now simply showing the matrix will put all columns next to eachother. If you want one column below, the trick is to reduce your windows width untill it can only hold five columns. Matlab will now print the last column below the first.
M % Simply show the matrix
% Now reduce your window size
M % Simply show it again
This should help you display the numbers in matlab, if you want to process them further you can consider to write them to a file instead. Try help xlswrite for a simple solution.
So I've got a .tcl file with data representing a large three-dimensional matrix, and all values associated with the matrix appended in a single column, like this:
128 128 512
3.2867
4.0731
5.2104
4.114
2.6472
1.0059
0.68474
...
If I load the file into the command window and whos the variable, I have this:
whos K
Name Size Bytes Class Attributes
K 8388810x3 201331440 double
The two additional columns seem to be filled with NaNs, which don't appear in the original file. Is this a standard way for MATLAB to store a three-dimensional matrix? I'm more familiar with the .mat way of storing a matrix, and I'm curious if there's a quick command I can run to revert it to a more friendly format.
The file's first line (128 128 512) gives it 3 columns. I don't know why there are 2so many extra rows (128*128*512 = 8388608), but your 3d matrix can probably be constructed like this:
N = 128*128*512;
mat = reshape(tab(2:N+1,1),[128 128 512]);
What's on the last hundred lines of the table that gets loaded?