Reading and Writing some parts of a line in another text file in a preferred format in Matlab - matlab

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!

Related

How can I print the ascii value of an input in Brainfuck?

What I want to do is for a Brainfuck code to print out the ascii value of the input. For example, typing in an input of "a" will give an output of 97. The python equivalent of this is print(ord(input())). What I'm thinking is that once I get the input with the , command, I can split the input value's digits into separate cells, and then print each cell individually. What I mean by this is let's say you type in an input of a. The , command will store the ascii value of a in the first cell(cell 0), which is 97 in this case. Then I run some algorithm that will split the 97 into its individual digits. So, in this case, cell 1 will have a value of 0(because 97 has a hundred digit of 0), cell 2 will have a value of 9, and cell 3 will have a value of 7. Then we can add 48 to each of those cells(0 has an ascii value of 48) and print each cell individually, starting from cell 1(the hundreds place). The problem I'm facing is writing the digit separation algorithm. I can't seem to make it work. My idea is to subtract 100 from the original number until that number is less than 100 while keeping track of how many times 100 has been subtracted, then repeatedly subtract 10, and finally we are left with the ones place. But the problem with this idea is that I have no idea how to track if the number falls under 100 or 10. Any suggestions or ideas? Thanks for the help in advance.
What you are trying to implement is called "divmod". divmod is a function that divides two numbers (in your case positive integers) and stores the result and the remainder. Implementations for this in brainfuck exist: Divmod algorithm in brainfuck
Good luck!

Comparing, matching and combining columns of data

I need some help matching data and combining it. I currently have four columns of data in an Excel sheet, similar to the following:
Column: 1 2 3 4
U 3 A 0
W 6 B 0
R 1 C 0
T 9 D 0
... ... ... ...
Column two is a data value that corresponds to the letter in column one. What I need to do is compare column 3 with column 1 and whenever it matches copy the corresponding value from column 2 to column 4.
You might ask why don't I do this manually ? I have a spreadsheet with around 100,000 rows so this really isn't an option!
I do have access to MATLAB and have the information imported, if this would be more easily completed within that environment, please let me know.
As mentioned by #bla:
a formula similar to =IF(A1=C1,B1,0)
should serve (Excel).

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

Matlab - Splitting a column into two (efficiently)

I had previously wrote some code to split 3 columns into 4, however the code was very inefficient and time consuming. As I am working with millions of rows it wasn't suitable. (Below is my previous code)
tline = fgetl(fid);
ID=tline(1:4);
IDN = str2double(ID);
Day=tline(6:8);
DayN = str2double(Day);
HalfHour=tline(9:10);
HalfHourN = str2double(HalfHour);
Usage=tline(12:end);
UsageN = str2double(Usage);
There must be a more efficient and quicker way of doing this?
Going back to basics, I have produced a x by 3 matrix. but require an x by 4 matrix
To show what I am trying to do, examining one row -
I am trying to change
1001 36501 1005
to
1001 365 01 1005
Any help would be much appreciated!
Edit:
The second column I am trying to divide into two, is always composed of 5 characters. I am trying to get the first 3 characters into their own column, likewise for the remaining characters.
What might take time in your case is actually the use of the str2double function. It is known that this built-in function becomes very slow when the data set is large. You might try to get rid of it if possible.
you can use modulo
ans = (36501 - mod(36501,100))/100
This would give you 365
if you want the 1, it is mod(36501,100)
so this would effectively split your second column into 2 different numbers, you can then re name them etc.
hmmm on second thoughts, if all your numbers on your second column are 5 digits, this can be extremely efficient, since mod is computed in matlab by b = a - m.*floor(a./m);
check http://uk.mathworks.com/help/matlab/ref/mod.html it should work for vectors (i.e. your second column)

read and rewrite in matlab

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