Values are getting converted to double automatically.

2 ビュー (過去 30 日間)
Madhurima Reddy
Madhurima Reddy 2023 年 9 月 29 日
コメント済み: Stephen23 2023 年 9 月 29 日

I have to load files having numbers in the filename. For example: CRH_0.5d_15.mat, 0.5d_15.mat. Following is the code I am using to access my files. The problem I am facing is, the values in the array are getting read as double type i.e. instead of b=0.5, it is taking b=0.5000. I tried using format short, but to no avail. Could someone help me with this? Thank you.

Ln= length(ab)
for l=1:1:Ln
b= ab(l)
a= load('CRH_',num2str(b),'d_15');
  4 件のコメント
Madhurima Reddy
Madhurima Reddy 2023 年 9 月 29 日

The file is getting read as CRH_0.5000d_15.mat, when it should be just CRH_0.5d_15.mat. Because of that, I am getting error saying file is not available in folder.

Stephen23 2023 年 9 月 29 日

Changing the format controls how numeric values are displayed in the command window, it has nothing to do with functions like num2str. If you want to have more control over the format whilst converting to text then use sprintf or similar.


回答 (1 件)

James Tursa
James Tursa 2023 年 9 月 29 日
Here is what I get just displaying the character string. I inserted brackets [ ] for concatenation:
Ln= length(ab);
for l=1:1:Ln
b= ab(l);
CRH_0.5d_15 CRH_0.75d_15 CRH_1d_15 CRH_1.25d_15 CRH_1.5d_15 CRH_2d_15
Maybe you just need the brackets [ ].


Help Center および File ExchangeTitle についてさらに検索




Community Treasure Hunt

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

Start Hunting!

Translated by