double zero 00 fill in array

22 ビュー (過去 30 日間)
Job de Vries
Job de Vries 2018 年 6 月 5 日
編集済み: Stephen23 2018 年 6 月 5 日
Hi All,
Just wondering how I can fill an column in a matrix with 00. Matlab automatically truncates to 0. I have the doublezero sprintf but this obviously is in the wrong format, i'd probably have to get it in integer form before I can insert it in an array called sec. But getting it in integer format truncates the value to a single 0. I looked around in the different formats but can't figure out which one to use.
doublezero = sprintf('%02d',0);
sec(1:60,1) = doublezero ;
str2doublezero = str2double(doublezero)
Thanks!
  1 件のコメント
Stephen23
Stephen23 2018 年 6 月 5 日
編集済み: Stephen23 2018 年 6 月 5 日
Numeric data classes do not store any formatting information whatsoever, so it is not possible to store leading zeros like this in a numeric array. As far as all numeric classes are concerned, 0==00==00000...
You could either store a char/string, or store the required number of digits in a separate variable. Note that storing numeric values as text usually makes code very complex and inefficient.

サインインしてコメントする。

採用された回答

Jan
Jan 2018 年 6 月 5 日
This is not possible. If sec should be a numerical array, that 00 is not meaningful - there is no such number and leading zeros are removed in general. Two zeros are meaningful as a string only. This is either a modern string object or a vector of type char:
c = '00';
s = "00"
If you need an array, this can be implemented as string vector, as char matrix or cell string: a cell containing char vectors:
sec = cell(60, 1);
sec(:) = {'00'}
But most likely storing seconds as numbers is much more useful. Then you insert the leading zero by sprintf only, if you want to convert it for a text output. Computations are much easier with numerical data.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by