필터 지우기
필터 지우기

repeat rows and export to txt file

조회 수: 2 (최근 30일)
Jacob Weiss
Jacob Weiss 2021년 7월 8일
댓글: Jacob Weiss 2021년 7월 9일
I read in a txt file containing n colums and n rows. I need to repeat each row x number of times and then export the new file to a txt document. I.E
A=importdata('file.txt')
A = [1;2;3;4]
x= 3
A = [1;1;1;2;2;2;3;3;3;4;4;4]
exportdata('NewFile.txt')
  댓글 수: 4
Mohsin Zubair
Mohsin Zubair 2021년 7월 8일
@Jacob Weiss I told you that how can you create your desired output but to export it, there are many ways to do so, it depends how or in whih format you want to export your data, also can you share your orignal text file with data?
Mohsin Zubair
Mohsin Zubair 2021년 7월 8일
well what I told is just using loops which isn't good for long file, what scott told you is much better for file of your size

댓글을 달려면 로그인하십시오.

답변 (1개)

Scott MacKenzie
Scott MacKenzie 2021년 7월 8일
편집: Scott MacKenzie 2021년 7월 8일
I think this is more or less what you're after:
% test data (read from file)
A = [1 8;2 7;3 6;4 5]
A = 4×2
1 8 2 7 3 6 4 5
x = 3; % number of times to repeat each row (change as desired)
A1 = repmat(A', x, 1);
A2 = reshape(A1, size(A,2), [])'
A2 = 12×2
1 8 1 8 1 8 2 7 2 7 2 7 3 6 3 6 3 6 4 5
% write to file
  댓글 수: 9
Scott MacKenzie
Scott MacKenzie 2021년 7월 9일
@Rik. Thanks. Yes indeed, much simpler. @Jacob Weiss with Rik's simplification, something like this will also work...
f = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/678488/Raj_AZ31_16120.txt';
A = readmatrix(f);
x = 3; % number of times to repeat each row (change as desired)
B = repelem(A, x, 1);
writematrix(B,'newfile.txt');
Jacob Weiss
Jacob Weiss 2021년 7월 9일
Great thank you for all the help! I have it working now.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by