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

470 visualizzazioni (ultimi 30 giorni)
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.

Risposte (2)

Raj
Raj il 10 Giu 2019
Modificato: Raj il 10 Giu 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

Utkarsh Belwal
Utkarsh Belwal il 10 Giu 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 Commenti

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by