tags:

views:

58

answers:

2

http://stackoverflow.com/questions/1905061/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

A: 

From what you tell us it is obvious only that you are trying to reshape a matrix (or other structure) from 84641x175 elements into an array which has a different number of elements, such as 84641x181. What you don't tell us is how you propose to cut out the excess elements in your array before you try to write it to file. It is also not clear why you are trying to use reshape at all.

But, if you had, in memory, an array A such that

size(A)

produces the result

84641 175

then a statement like this

B = A(:,1:40);

would create an array with the right number of elements for your output file. Without seeing some of your code, this is just educated guesswork of course.

High Performance Mark
All i want is the same file with only first 40 columns without changing any format.from the code i am just modifyingfid = fopen('newfile1.txt','wt'); %# Open the file fprintf(fid,'%s',topLine); %# Print the top line for i = 1:size(data,1) %# Loop over the rows of data fprintf(fid,'%d/%d/%d, %d:%d:%d',data(i,1:6)); %# Print the date fprintf(fid,', %.1f',data(i,7:40)); %# Print the data fprintf(fid,'\n'); %# Print a newline end fclose(fid); %# Close the file
Paul
+1  A: 

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
gnovice
Another question how do i selectively pick the headers?i am doingfprintf(fid,', %.1f',data(i,7:46));fprintf(fid,', %.1f',data(i,47:50));and some more how do i pick the headers and write?when i trycolumnHeader = {'col1','col2','col3'};fprintf(fid,'%s',columnHeader); ??? Undefined function or method'fprint' for input arguments oftype 'cell'.Error in ==>fprint(fid,'%s',columnHeader);
Paul
@Paul: To print your cell array of header labels, you could do this:`fprintf(fid,'%s\t',columnHeader{:}); fprintf(fid,'\n');`. You could also replace the `\t` after the `%s` with one or more spaces, if that makes your output file look better.
gnovice

related questions