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
Related
I have to read different numbers in the same line in a text file. How can I pass them to an Array (for each line), if I don't know how many numbers I have to read?
I thought about reading each number and passing it to an array, until I find the New Line character. But I have a lot of files, so doing this takes a lot of time.
With this arrays from each file I have to build plots. Is there any other way?
12 43 54 667 1 2 3 1 545 434 6 476
14 32 45 344 54 54 10 32 43 5 6 66
Thanks
You can open each file and read it line by line, then use textscan(str,'%d') to convert each line into an array.
Example for one file:
fid = fopen('file.txt');
tline = fgetl(fid);
while ischar(tline)
C = textscan(str,'%d');
celldisp(C);
tline = fgetl(fid);
end
fclose(fid);
You would have to run the code for each file, and do something with the array C.
You can read the additional details on the function textscan.
The way to read ASCII-delimited, numerical data in MATLAB is to use dlmread, as already suggested by #BillBokeey in a comment. This is as simple as
C = dlmread('file.txt');
I do appreciate any detailed helps. I really am in a terrible situation and I would be honored if anyone can help me with this issue in a great details! Thanks in advance!
Well! I have a large text file that is made of group of 209 rows! In another words in my large file there is simple element with the following format that repeats many times (let us name it NR) . each element has 209 rows and 5 columns. I am interested in having the data corresponding to the last three columns for 6 specific rows in each element. these 6 rows ( let me call them r1 to r6) are constant for all of the NR loops.
The third column which is the first column of interest starts at character number 25 of the row i.e. cell number 25 and ends at character number 37.
The forth column which is the second column of interest starts at character number 51 of the row i.e. cell number 51 and ends at 63.
The fifth column which is the third column of interest starts at character number 77 of the row i.e. cell number 77 and ends at 89.
I need to create NR separated text files and have the data of interest be written in the following format for each of the NR loop:
1) For each file the first 16 lines (rows) there is a similar text that is required to be at each file. For example :
"Thank you for your help!
I do appreciate it
and so on "
2) from Line 17 to Line 22 I need to print the data that has been read previously for r1 to r6 respectively, such a way that information of third column is being printed at character (cell) number 24, information of forth column is being printed at character (cell) number 40 and information of fifth column is being printed at character (cell) number 56.
3) for lines 17-22 I need to add four new columns at cells number , 4,8,12 and 16 respectively such that
A) the first column is 1 for r1, 2 for r2 and etc.
B) The second column is 1 for r1 and r2 and 2 for the r3 to r6
C) the third column is the same as second column
D) the forth column is always 0.
Wow! I know it might be so hard to get the point with the text :D
I hope you could help me with that !
So just to sum Up the points. I need NR separated files which names are from 1 to NR. Each of these NR files are related to the same loop in my large file.
Thanks!
All the best!
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.
How do I write a text file in the same format that it is read in MATLAB?
I looked and my question is almost the same as above question.
I want to read in a file which is 84641 x 175 long.
and i want a make a new .txt file with 84641 x 40 , deleteling rest of the columns.
I have 2 rewrite the dates n times. date is on first column in format 6/26/2010 and time on 2nd column in format ' 00:00:04'
when i use the code put in above question i keep getting the error
??? Error using ==> reshape
Product of known dimensions, 181,
not divisible into total number
of elements, 14812175.
Error in ==>
write at
data = reshape(data{1},N+6,[])';
when i comment this it has error in printf statements for date and data write.
Any ideas??
thanks
As the author of the accepted answer in the question you link to, I'll try to explain what I think is going wrong.
The code in my answer is designed to read data from a file which has a date XX/XX/XXXX in the first column, a time XX:XX:XX in the second column, and N additional columns of data.
You list the number of elements in data as 14812175, which is evenly divisible by 175. This implies that your input data file has 2 columns for the date and time, then 169 additional columns of data. This value of 169 is what you have to use for N. When the date and time columns are read from the input file they are broken up into 3 columns each in data (for a total of 6 columns), which when added to the 169 additional columns of data gives you 175.
After reshaping, the size of data should be 84641-by-175. The first 6 columns contain the date and time values. If you want to write the date, the time, and the first 40 columns of the additional data to a new file, you would only have to change one line of the code in my answer. This line:
fprintf(fid,', %.1f',data(i,7:end)); %# Output all columns of data
Should be changed to this:
fprintf(fid,', %.1f',data(i,7:46)); %# Output first 40 columns of data
I have a 2-by-3 matrix, and I want to sort it according to the first column. Here's an example:
data will change to --> new data
11 33 10 22
22 44 11 33
10 22 22 44
I have this code for sorting a matrix A but it doesn't work well:
sort(A,1,'ascend');
The SORTROWS function can handle that for you:
B = sortrows(A);
As #gnovice suggests, sortrows is the best solution here. You can also specify more than one output for the sort and sortrowscommands, which will return the sort index. You can use this to modify your other columns as well or just to keep track of the permutation. For example:
A=rand(10,2);
[B, idx]=sortrows(A);