Reputation: 7
I would like to use MATLAB to write two vectors of numbers into a file and obtain a result like that:
0 ; [ 0]
1 ; [ 1]
2 ; [ 2]
3 ; [ 3]
...; [ ...]
I tried and obtained that code:
h=[0:1:100];
a=[0:1:100];
formatSpec = '%3d ; [%5d]\r\n';
fileID = fopen('Write_in_file.txt', 'w');
fprintf(fileID,formatSpec,h,a)
Unfortunately, here's the result of my code :
0 ; [ 1]
2 ; [ 3]
4 ; [ 5]
6 ; [ 7]
Could you help me to get the result that I want? I don't know how to do that actually...
Upvotes: 0
Views: 40
Reputation: 14371
You'll have to concatenate a
and h
into one array. Otherwise fprintf
will first go through all numbers of h
, and then through all numbers of a
. As MATLAB takes the values column-wise, you need to make sure that the vector looks like this:
0 1 2 3 ...
0 1 2 3 ...
which can be done with [h;a]
. So, just call
h=[0:1:100];
a=[0:1:100];
formatSpec = '%3d ; [%5d]\r\n';
fileID = fopen('Write_in_file.txt', 'w');
fprintf(fileID,formatSpec,[h;a])
Upvotes: 1