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?
Related
I have an issue with saving and loading a huge dataset in Matlab.
My dataset contains properties of series of images using Matlab's regionprops.
I currently have a MAT-file of about 21GB and this takes a while to load.
This MAT-file has one cell array containing structure arrays of the properties of ellipses on each slice.
Are they any suggestions as to how to go around this?
Is there any better and efficient way of saving MAT-files than the -v7.3 formats?
One solution could be to use the 'table' argument to regionprops. This causes the output to be a table rather than a struct array. This format is more efficient for storage than the struct array.
Better yet, if you don't mind manually keeping track of what data is where, is to create a numeric array with the relevant data:
BW = imread('text.png'); % Example image used in the docs
s = regionprops(BW,{'MajorAxisLength','MinorAxisLength','Orientation'});
t = regionprops('table',BW,{'MajorAxisLength','MinorAxisLength','Orientation'});
m = [s.MajorAxisLength; s.MinorAxisLength; s.Orientation];
whos
Name Size Bytes Class Attributes
BW 256x256 65536 logical
m 3x88 2112 double
s 88x1 31872 struct
t 88x3 3496 table
A numeric array is a much more efficient way of storing data than a struct array, because each element in the struct array is a separate matrix, which needs its own header. The header (114 bytes I believe) in this case is far larger than the value stored in the array (8 bytes in this case), hence the overhead of 31872 / 2112 = 15.1.
The table stores each column in a separate array, so there you have a much smaller overhead. Instead of having 3 x 88 (number of features x number of objects) arrays, you have only 3.
If each image is guaranteed to have the same number of objects, you could consider putting these matrices into a single 3D array instead of a cell array. The gain here would be smaller.
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!
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 have 60 values in a cell matrix from a .csv file which are stored in column 15 from rows 38556-38616
I want to copy that range of values to a regular numerical 60x1 matrix in a variable Value
Here is what I tried:
Values(60,1) = data2{38556:38616,15};
It only copies 1 value from data2 and the rest of values from rows 1-59 are 0. How can I copy
those values to Value in the same order they are in the file? data2 is the cell matrix storing .csv file values.
Image of data2:
Either
Values(1:61,1) = cell2mat(data2(38556:38616,15));
or just
Values = [data2{38556:38616,15}]';
The problem was that you tried to store 61 values into just one matrix element.
Incidentally, if you don't need the full contents of the csv file, just a subset, and the contents you are reading in are numeric, you can take this directly from the file using dlmread. For example:
values = dlmread('data.csv',',',[38555 14 38615 14]);
The numeric values are starting and ending positions: [R1 C1 R2 C2]. dlmread starts counting at zero for R/C values. You can also use spreadsheet style instead, i.e. 'A4..G6' or in your case I think it would be 'O38556..O38616'.
I have a matrix say ABC which has 60 rows and 120 columns.
Depending on another matrix X which is a 120 entry long array I would like to populate another matrix as follows:
if X(i)=1 column i is added to the matrix ABC_Copy.
if X(i)=0 column i is skipped the loop continues.
As it is obvious i would iterate from 1 to 120 which is the size of S representing the 120 columns in ABC.
How can we implement this in matlab without iterating entirely and placing each value individually?
You can use logical arrays for indexing in Matlab:
ABC_Copy = ABC(:, X==1);