Info

This question is closed. Reopen it to edit or answer.

I am trying to import a number specified by a cell position from a XYZ1.m file into another XYZ2.m file. the value of the cell in XYZ1.m is 1.0 but when i import it in XYZ2.m it imports it as 1. I want to import as 1.0. What should i do?

1 view (last 30 days)
In XYZ1.m
i = 1;
Cell position [3 i 3 i] is 1.0
In XYZ2.m I read,
wave_amplitude = dlmread(fid,'',[3 i 3 i]);
then it prints out,
wave_amplitude = 1;
How to make it read as 1.0?
Thank you

Answers (1)

Walter Roberson
Walter Roberson on 17 Jul 2015
fprintf('%.1f', wave_amplitude);
The 1 you are seeing is identical to 1.0, and it is just a matter of how it is displayed.
  3 Comments
Walter Roberson
Walter Roberson on 17 Jul 2015
The output of fprintf() is the number of characters printed. If you want the string use sprintf() instead.
fileID = sprintf('time_series_%.1f_%d.mat', wave_amplitude, wave_period)
Note: if the input might have more digits beyond 1.0 such as 1.25 then it would be necessary to change this code.

Community Treasure Hunt

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

Start Hunting!