フィルターのクリア

read binary text from a file and add zeros to it

2 ビュー (過去 30 日間)
Hadeel
Hadeel 2022 年 9 月 11 日
コメント済み: Hadeel 2022 年 9 月 11 日
Please. I need to read binary text from a file and if it is not a multiple of a certain number, add zeros to te until it becomes a multiple of it,iam do tracing to my code and i discover that the statment a=......... in while loop donot change its value based on the new text,it remain on the first text.
text = fileread('text1.txt');
n=input('enter the length of segment: ');
a=mod(length(text),n);
if a~=0
while a~=0
text=string(text)+"0";
a=mod(length(text),n);
end
end

採用された回答

Image Analyst
Image Analyst 2022 年 9 月 11 日
You forgot to attach the file!
If the text is binary like 0s and 1s, maybe try this
b = logical(readmatrix('text1.txt'));
% Make the vector 500000 elements long by padding zeros.
b(5000000) = false;
If you have any more questions, then attach your data with the paperclip icon after you read this:

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2022 年 9 月 11 日
if a~=0
text = [text, repmat('0',1,n-a)];
end
Your bug is that you are converting text to string() array, but length() of a string array is the number of strings in the array, not the number of characters in the entry. See also strlength()
  1 件のコメント
Hadeel
Hadeel 2022 年 9 月 11 日
thank you so much for your answer.

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


dpb
dpb 2022 年 9 月 11 日
移動済み: Image Analyst 2022 年 9 月 11 日
What's the form of the file -- just a string of 0s and 1s? if so then it would be simply something like
text = fileread('text1.txt');
N=input('enter the length of segment: ');
text=strcat(text,'0',repmat(1,N-strlength(text)));
if N > strlength(text), thanee it will add enough '0' to make up N overall; repmat will silengly do nothing if the repeat count is <=0 so you don't need any other logic outside; it's all handled internally ("magic happens").
fileread returns a char() vector to begin with so there's no conversion required there, either.
  3 件のコメント
Walter Roberson
Walter Roberson 2022 年 9 月 11 日
User wants output to be a multiple of N long rather than padding <N to N and leaving >N unchanged
Hadeel
Hadeel 2022 年 9 月 11 日
thanks for your answer.the matters became ok

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

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by