How I can use printf or disp in MATLAB to print some special format of my data set?
482 次查看(过去 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!