How can I limit the number of characters to <=132 characters per line in the text output file (characters include space, commas, periods, etc.) ?

39 ビュー (過去 30 日間)
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 件のコメント
Walter Roberson
Walter Roberson 2017 年 12 月 11 日
fmt = sprintf('%%%d.%df', L, Decimal);
fprintf(fileID, fmt, A(i));
Yildirim Kocoglu
Yildirim Kocoglu 2017 年 12 月 11 日
That worked Walter. Thank you so much!
I tried to write '%d.f' and it wouldn't work --> Just for my own understanding --> Why is that? And how do I mark your answer as the best answer?
Would really like to help you if marking it as best answer helps or whatever that needs to be done for you to get benefits out of this as well...

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2017 年 12 月 11 日
fmt = sprintf('%%%d.%df', L, Decimal);
fprintf(fileID, fmt, A(i));
  2 件のコメント
Yildirim Kocoglu
Yildirim Kocoglu 2017 年 12 月 12 日
Thanks again Walter. In %%%d, what does %%% do? I know that must be a basic question. My apologies... I realized it only works with %%% & not % or %%. That's the reason I am asking.
Walter Roberson
Walter Roberson 2017 年 12 月 12 日
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'];

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by