How can I limit the number of characters to <=132 characters per line in the text output file (characters include space, commas, periods, etc.) ?
    6 views (last 30 days)
  
       Show older comments
    
    Yildirim Kocoglu
 on 10 Dec 2017
  
    
    
    
    
    Commented: Walter Roberson
      
      
 on 12 Dec 2017
            I am reading from an excel sheet into matlab & I have a large data file.
I am trying to find a way to write an output text file with maximum of 132 characters per line without separating the original element (even if I use another set of data with different digit size numbers (such as 123 instead of 42255123451234).
I want the code to work no matter how long the number is --> as long as each element of the matrix is smaller than 132 characters in a single line--> I don't have anything greater than 132 characters per element to be clear).
For example: If the last element is going to make the characters per line more than 132 it should move to the next line instead (if that makes sense).
I already sort of did this but each element is in a column & the code is not efficient enough.
I am going to import these numbers into another software & copying these large number of columns as such can become really annoying to go through in that software since it has much more data than the original attached.
I have attached what the file looks like & I used this code below to write it:
clear;
clc;
A=xlsread('Data.xlsx', 'Numbers', 'A3:A214');
fileID=fopen('Data', 'w');
fprintf(fileID, '%14.0f,\r\n', A); 
fclose(fileID);
Please let me know if you have further questions to clarify what I am asking for --> I tried to be as clear as possible but, I might not have been able to explain it very well.
Any help is appreciated. Thank you.
5 Comments
  Walter Roberson
      
      
 on 11 Dec 2017
				fmt = sprintf('%%%d.%df', L, Decimal);
fprintf(fileID, fmt, A(i));
Accepted Answer
  Walter Roberson
      
      
 on 11 Dec 2017
        fmt = sprintf('%%%d.%df', L, Decimal);
fprintf(fileID, fmt, A(i));
2 Comments
  Walter Roberson
      
      
 on 12 Dec 2017
				The breakdown of '%%%d.%df' is '%%' '%d' '.' '%d' 'f' .
The '%%' instructs sprintf to output '%' -- because % is special to sprintf() you need a way to indicate that you want % itself to be output, and the way to do that is to use %% .
The %d after that means that the first parameter (L) is to be output to the string as a decimal number.
The . after that means that '.' is to be output to the string.
The %d after that means that the second parameter (Decimal) is to be output to the string as a decimal number.
The f after that means that 'f' is to be output to the string.
The resulting string will start with '%' followed by the representation of L, followed by period, followed by the representation of Decimal, followed by 'f'. For example %9.3f might be the resulting string.
Then this string is taken and used as the format in fprintf(), which would interpret it as indicating a certain minimum total length and a certain number of digits after the decimal point are to be output.
Another way of writing
fmt = sprintf('%%%d.%df', L, Decimal);
would be
fmt = ['%', num2str(L), '.', num2str(Decimal), 'f'];
More Answers (0)
See Also
Categories
				Find more on Characters and Strings in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!