MATLAB: Write a function that takes the name of a text file and returns a 2-dimensional array of the data in the file, skipping the first row of data? - matlab

a. I need help to write a function that takes the name of a tab delimited text file and returns a 2-dimensional array of the data in the file, skipping the first row of data?
b. Using the same function from the first part I need to write another function maxMerge that takes the name of two of these tab-delimited files and returns a single 2-dimensional array where each element of the array is the larger of the corresponding elements from the two data files.
I would appreciate any help....Thank You!
example:
file 1 file 2 maxMerge
0 0 0 0 0 0
10 20 30 2 4 8 10 20 30
45 55 63 16 32 64 45 55 64
80 90 99 128 56 500 128 90 500

This reads a little like homework so I'll point you in some directions. The TEXTSCAN function will read the contents of a file. Have a look at the HeaderLines option for skipping the first row.
In the second part, you could do the following:
Use the function of the first part to load the two data files of size Nx2.
Concatenate the two loaded arrays so you have a Nx2x2 array.
Use the MAX function with the dimension argument to find the maximum of the concatenated array along the 3rd dimension, i.e the dimension which designates unique data files.

Related

Adding a column to a .csv in MATLAB

I have a nxn .csv file in which I am finding the cumulative sum of one column. I need to append this column with a header cumsum to the end of the existing .csv file to make it nx(n+1). How could this be done? I am attaching a samaple:
filename A B
aa 23 34
aa 56 98
aa 8 90
aa 7 89
I am finding the cumsum of column A
23
79
87
94
I need this column appended to the end of .csv as
filename A B cumsum
aa 23 34 23
aa 56 98 79
aa 8 90 87
aa 7 89 94
I have 2 problems here:
1. I am extracting the column A everytime to perform the cumsum operation. How do I find it directly from the table for a single column without extraction?
How do I create a new column at the end of the existing table to add the cumsum column with a header 'cumsum'?
For point 1: You can use csvread to read a specific column directly from a .csv file without loading the whole thing. For your example, you would do this:
A = csvread('your_file.csv', 1, 1, [1 1 nan 1]);
The nan allows it to read all rows until the end (although I'm not sure this is documented anywhere).
The use of csvread is applicable to files containing numeric data, although it works fine for the above example even with character entries in the first row and first column of the .csv file. However, it appears to fail if the part of your file that you want to read is followed by columns containing character entries. A more general solution using xlsread is as follows:
A = xlsread('your_file.csv', 'B:B');
For point 2: Built-in functions like csvwrite or dlmwrite don't appear able to append new columns, just new rows. You can however use xlswrite, even though it is a .csv file. Here's how it would work for your example:
xlswrite('your_file.csv', [{'cumsum'}; num2cell(cumsum(A))], 1, 'D1');
And here's what the contents of your_file.csv would look like:
filename,A,B,cumsum
aa,23,34,23
aa,56,98,79
aa,8,90,87
aa,7,89,94

Convert specific txt to mat file in Matlab

How can I convert a txt file (that has n rows of data) with the following format:
"header1","header2","header3"
1.20,2,3
2.03,3,4
1.05,5,6
8.20,9,4
etc.
into a mat file that's simply a 2xn matrix that ignores the first row and first column of the text file and essentially transposes the other columns:
2 3 5 9
3 4 6 4
There are many ways to do that, but one function in particular allows starting the read at a given row and column offset simply in the calling parameter (without having to specify headerlines or adding * in format specifier): The function dlmread
In your case, it's a simple matter of calling it with the right parameters:
M = dlmread( 'test.txt' , ',' , 1 , 1 ).' ;
M =
2 3 5 9
3 4 6 4
The nice thing about this function is it returns a double array directly (useful if you only want to read numbers), instead of a cell array like some other functions.
Note that I transposed the result at the end of the line (using the transpose operator .'), to get it the way you wanted.
Also note that the last 2 parameters are considered offset (as opposed to start row index). It means a value of 1 specify an offset of 1, so the reading will start at row index 2 (first index is 1).

Import a txt weighted adjacency list into a matrix

I want to create a weighted adj matrix. Is there a good way that will work even with a huge data set?
I have this abc.txt file for example:
abc.txt
1 2 50
2 3 70
3 1 42
1 3 36
result should be
matrix=
0 50 36
0 0 70
42 0 0
How can I construct a weighted adjacency matrix from input dataset graph file as shown above that will contain the weights?
So basically input file has 3 columns and the third column is the weights of each edge.
You could also apply spconvert to the output of importdata:
matrix = full(spconvert(importdata('abc.txt')));
What you have is a sparse definition of a matrix, using sparse is the simplest way to create it. If your matrix is thin (many zeros) you may also stick with the sparse matrix because it requires less memory. Then delete the last line.
S=load('abc.txt')
M=sparse(S(:,1),S(:,2),S(:,3))
M=full(M)

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