MATLAB Answers

sherek
0

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

Asked by sherek
on 10 Jun 2019
Latest activity Edited by sherek
on 10 Jun 2019
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 Comment

When you edit your post away, you insult the person who chose to spend their time to answer your question.

Sign in to comment.

2 Answers

Answer by Raj
on 10 Jun 2019
Edited by Raj
on 10 Jun 2019

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 Comments

Sign in to comment.


Answer by Utkarsh Belwal on 10 Jun 2019

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 Comments

It still print the head of my file in each time. (which is the name of variables ) is there any way to remove it?
What is the purpose of editing the question to nonsense??

Sign in to comment.