How do I use %d to get a column of numbers in %d format?

Hi, I have the following code, but it isn't working:
x=0.05:0.001:0.25;
for i=1:201
data(i,1) = ('%d',i);
end
for i=1:201
data(i,2) = x(i);
end
save 'data.dat' data -ascii;
I want 201 rows of data, where the first column contains 1 2 3 ... (in %d format) while the second column contains values of x in %e format. How can I do this?
Thank you very much in advance!

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 16 Okt. 2017
Bearbeitet: Walter Roberson am 16 Okt. 2017
x=0.05:0.001:0.25;
for i=1:201
data{i,1} = sprintf('%d',i);
end
for i=1:201
data{i,2} = sprintf('%e', x(i));
end
However, this creates cell arrays of character vectors, and those cannot be saved using save -ascii .
You would be better off going for writing them yourself:
fid = fopen('data.dat', 'wt');
fprintf( fid, '%3d %e\n', [1:201; x]);
fclose(fid)

3 Kommentare

Thanks!!!
Can someone please explain me what is the use of %d in matlab?
Inside fprintf() and sprintf() and compose(), % followed by a sequence of characters is a "conversion specifier", indicating that the % sequence is to be replaced on output by the converted value from the corresponding additional parameter. There are options for a %d specification that are rarely used, which I will not discuss here. The commonly used versions are:
  • %d -- convert an integral number to a sequence of digits, with leading - (negative sign) if necessary. No decimal place or exponent are used, even for large integers. The fewest characters needed to output the number are used: no leading or trailing spaces or zeros are used.
  • %NUMBERd such as %3d -- convert an integral number to a sequence of digits, with leading - (negative sign) if necessary. No decimal place or exponent are used, even for large integers. At least the number of characters given by NUMBER are used, with the output being prefixed by blanks if needed. If more digits are needed, then they will be used. For example %2d format asked to output 999 will output '999'. It is not an error of any kind to output a number with more digits than are indicated in the NUMBER.
  • %0NUMBERd such as %03d -- convert an integral number to a sequence of digits, with leading - (negative sign) if necessary. No decimal place or exponent are used, even for large integers. At least the number of characters given by NUMBER are used, with the output being prefixed by zeros if needed. If more digits are needed, then they will be used. For example %2d format asked to output 999 will output '999'. It is not an error of any kind to output a number with more digits than are indicated in the NUMBER.
If there is a + somewhere between the % and the NUMBER, then the sign will always be output even if it is + . For example %+03d asked to print 2 will produce '+02' (the plus or minus counts as one of the characters given by NUMBER.)
There are other options as well, and other format specifiers, such as %f .

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Powertrain Blockset finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by