How I can use printf or disp in MATLAB to print some special format of my data set?

조회 수: 259 (최근 30일)
sherek
sherek 2019년 6월 10일
편집: sherek 2019년 6월 10일
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
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
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

Utkarsh Belwal
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
madhan ravi
madhan ravi 2019년 6월 10일
What is the purpose of editing the question to nonsense??

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by