How I can use printf or disp in MATLAB to print some special format of my data set?
조회 수: 259 (최근 30일)
이전 댓글 표시
I have a data set with 5 columns and 668 rows. I need to use these data in ampl and I need a special format of it as the following :
1 3 4 5 7
5 4 3 2 1
4 5 6 4 3
4 5 3 4 2
[*,*,1]: 1 2 3 4:=
4 3 2 1 5
4 5 6 7 4
3 4 5 6 7
3 4 2 3 1
[*,*,2]: 1 2 3 4:=
4 5 6 2
4 3 2 1
4 5 3 2
1 2 7 1
[*,*,3]: 1 2 3 4:=
.
.
.
In other words, I have to print 4 rows then [*,*, i]: 1 2 3 4:= again 4 rows and that statement and so on. It should be done by a simple for loop but I don't know how to do that since I don't work with MATLAB.
Notice that I have a csv file and matlab gave a variable name to each column of my data. I don not want that head to be printed.
댓글 수: 1
John D'Errico
2019년 6월 10일
When you edit your post away, you insult the person who chose to spend their time to answer your question.
답변 (2개)
Raj
2019년 6월 10일
편집: Raj
2019년 6월 10일
A=ones(668,5); % Put your matrix here
fid=fopen('MyFile.txt','w'); % Open text file
temp=1;
temp1=1;
for ii=1:668
if mod(temp,5)==0
fprintf(fid,'[*,*,%i]: %i %i %i %i:= \r\n',temp1,[1 2 3 4]);
temp1=temp1+1;
else
fprintf(fid,'%i %i %i %i %i\r\n',A(ii,:));
end
temp=temp+1;
end
fclose(fid); % Close the file
댓글 수: 0
Utkarsh Belwal
2019년 6월 10일
A simple implementation is using a for loop and at each multiples of four print the required statement. Here is the code for same,
row_size = 668 ;
column_size = 5 ;
for i = 1 : row_size
disp(array_name(i , :))
if mod(i,4) == 0
disp(sprintf('[*,*,%d]: 1 2 3 4:=' , i / 4))
end
end
Replace the variable array_name with your array.
댓글 수: 3
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!