Copying cell matrix values to new matrix MATLAB - matlab

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'.

Related

how to store data without dynamically naming variables

I have 40 variables. The 40 variables names are in a cell array (40 x 1).
Each variable will have a matrix. The matrix is of type of double and will be size 5000 x 150. It will also have a vector of size 1 x 150 & one last vector 1 x 4.
Initially I was going to dynamically name each struct with its variable name in the cell array. So would look like something like below (assuming variable name is ABC),
ABC.dataMatrix
ABC.dataVec
ABC.summaryData
All the variables would be saved to a directory.
However I've read using eval isn't a very good idea so guessing my idea isn't the best way to go about this problem. What would be the best way to go about this problem?
You can either use struct arrays with dynamic field names, as #Shai and #RobertStettler suggest.
However, another option is a table. This might be more appealing if you want to see your data in one big matrix, and you can give each table row the name of your variables too! Note that the rows in a table would then be what you call your variables, but MATLAB calls the table columns its variables.
Also note that using a table can be more difficult than using struct or cell arrays, but if you know how to use them, you can handle a table too.
An example:
% create dummy data
rowNames = {'a';'b';'c'};
M = {ones(3); zeros(3); nan(3)}; % a cell, one element per item in rowNames
V = [1 2; 3 4; 5 6]; % a matrix of vectors, each row containing a vector for every item in rowNames
% create a table
T = table(M,V,'RowNames',rowNames); % this is where your variable names could go
Now, to access data you could use (some examples):
T(2,:) or T('b',:), return a table for all data on the 'b' row.
T(:,2) or T(:,'V'), return a table of variable V for all rows.
T.V or T{:,2} or T{:,'V'} or T.(2), return matrix V for all rows. This syntax is similar to accessing a (dynamic) field name of a struct.
T{3,1} or T{'c',1} or T.M('c'), return cell M for row 'c'. This syntax is similar to accessing a cell, but with more advanced possibilities, i.e. the ability to access the table via row or variable names.
T{3,1}{:} or T{'c',1}{:} or T.M{'c'}, return cell contents M for row 'c'.
And even more complex: T('a',:).M{:} is a complex way of accessing the cell content of M for row 'a', which can be done with T{1,1}{:} or T.M{'a'} or T{'a','M'}{:} or T.M{1} as well.
In your case you would en up with a 40x3 table, with every row what you call a variable and the first column the matrices (in cell arrays), and the last two columns the vectors (as well in cell arrays or as a 40xm double, m being the length of your vector).

Matlab xlsread: force empty cells to be read as NaN

I need to import data from a square region (10 by 10 cells) on an Excel sheet into Matlab.
All data in the region are numerical, but some outer rows and columns of the region are empty.
In Matlab I still want to have a 10 by 10 matrix of doubles with NaNs in places where there are empty cells in Excel (also in outer rows and columns).
If I use xlsread then empty outer rows and columns are automatically truncated.
Needless to say that all should be done automatically without the knowledge how many empty outer rows and columns are there.
How can I do this?
Let's say your 10 by 10 spreadsheet's first row and column and last row and column are empty (like this). Using:
[num,txt,raw] = xlsread('myfile.xlsx',1,'A1:J10'); % Read input.
will return:
num 8x8 double
txt 0x0 cell
raw 10x10 cell
In num, non-scalar leading rows and columns are automatically truncated, while in txt any numerical values are omitted. However, raw contains all information, so it can be used to extract the numerical values:
raw(cellfun(#ischar,raw)) = {NaN}; % Set non-scalar values to missing.
A = cell2mat(raw); % Convert to matrix.

Sum most recent entries of an ever-increasing .csv file MATLAB?

How can I sum the 100 most recent values in the second column of a .csv file?
The .csv file is constantly updated with a new row every minute, containing a new value. i.e the row dimension is forever increasing every minute. Assume I may accumulate 200 values, i.e 200 rows of data after 200 minutes (from external processes) but I only want to sum the 100 most recently added values (i.e from the last row of the file backwards up 99 rows)).
I have 3 sets of values, 1 value in each column. Col2Value is the value in Column 2 of the .csv file. The file i'm appending to is called '200 mins.csv'
dlmwrite('200 mins.csv', [Col1Value, Col2Value,Col3Value], '-append');
Here's a snippet of the csv file after 8 minutes of run-time i.e 8 values in column 2:
I know that MATLAB has to read the whole file first, but that's fine. I can deal with that, my application is not resource critical.
When you have an array in Matlab, you can sum the last 100 elements with
last100sum = sum(A(end-99:end));
So if reading in your .csv file gives you a 2D array (which I will call wholeFile), and you want the sum of the last 100 elements in column 2, you do it like this:
last100sum2 = sum(wholeFile(end-99:end, 2));
If the routine you use for reading the file returns a cell array, you need to convert the cell array to a regular array first - perhaps using cell2mat if it contains only values (as opposed to strings, for example).
Finally - if you want to read "at most" 100 values, but there might not be 100 rows, then do the following:
sz = size(wholeFile);
firstRow = max(sz(1) - 99, 1); % will return 1 if fewer than 100 rows are found
atMost100sum = sum(wholeFile(firstRow:end, 2));
Any questions - please ask.

Copy columns from a matrix to another matrix in matlab

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);

Single-Column Matrix Indexing

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?