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
Related
I need to ingest a CDF (common data format) file into MATLAB. I have used the [cdfread][1] command for this purpose. An image of my output is attached below:
When I open data_import, columns 4 and 5 are in a particular 3 x 1 format (as shown in data_import(1,4)).
My question is: Is there a simple way to extract the data for each cell in column 4, such that for the 2nd row in data_import(1,4), it gets inserted as a new column (i.e. column 5) in the original data (data_import)? Similarly, 3rd row in data_import(1,4) should be inserted as a new column (column 6) in the original data (data_import). This procedure should also be repeated in the original Column 5 data which also has a similar 3 x 1 structure within each cell.
I hope I'm not being too vague in what I am describing, but I'm really not sure what I'm supposed to do regarding the commands to call for the operation. Thank you in advance.
Your desired final output has columns which are made up of these cells converted from 3 x 1 arrays to 1 x 3 cell arrays and then concatenated for each row. It's easier to do the concatenation first with the elements the "wrong way round" and then transpose the final result:
data_import = [data_import(:,1:3) num2cell([data_import{:,4}; data_import{:,5}]') data_import(:,6:end)];
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!
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.
I have an excel worksheet with several entries of column data. The data is arranged in pairs such that the first column contains dates and the second contains time series data corresponding to that date. So for example time series 1 will be in columns A and B where is is the dates and B is the data. Column C is blank before columns D and E contain the entries for time series 2 and so on and so forth...
How do I merge these into a single file in Matlab where the dates match up? Specifically I would want the first column to contain the dates and the other columns to contain the data. I have tried to do this with fts and merge functions but so far failed..
You could grab the dates like this: dates = [raw{:,1}]' and the data like this data = reshape([raw{:, 2:3:end}]', size(raw,1), []); to get normal matlab matrices in case you want to manipulate them in matlab.
Otherwise if you just want to send them straight back to excel then:
data = [raw(:,1) reshape(raw(:, 2:3:end)];
xlswrite(...blablafilename_etc..., data);
But in this case you should have use a VBA macro :/
I have an input data in Excel which has 2000 rows and 60 columns.
I want to read this data into MATLAB but I need to to interchange the rows and the column so that the matrix will be 60 rows and 2000 columns. How can I do this in MATLAB, because Excel only has 256 column which cannot hold 2000 columns.
You just need to transpose it: data = data'
To read in the data to MATLAB, start with the xlsread function. Then transpose it, as tzaman showed in his solution.
Your code might look like this:
[filename,path]=uigetfile();
data=xlsread([path,filename]);
data=data';
xlswrite([path,'myfile.xls'],data);
Which would save the transposed data as myfile.xls in the same directory as the original file.
EDIT: Excel 2003 is limited to 256 columns, which is why xlswrite is throwing an error. Have you tried using dlmwrite instead?