How do i make up a word by attaching several words in Matlab?
Show older comments
so i want to input a value, and this value should be attached to certain words to do the next command.
For example, I have file names as 1.0p.txt, 2.2p.txt, 3.7p.txt, 10.09p.txt .... xp.txt where x is a number.
If I input the number, i want the m-file to combine the inputted number to p.txt to make xp.txt
so if i input 5.543, the m-file should give me 5.543p.txt and so i can use the file.
How can i do that?
Accepted Answer
More Answers (1)
Walter Roberson
on 26 Jan 2016
When you enter a number, it is not possible to determine afterwards how many 0's should follow the decimal place. For example you might enter 5.5500 as a number, but MATLAB would not be able to tell whether you had entered 5.55 or 5.550 or 5.5500 and so on. As your filenames may include trailing 0s and do not have a fixed number of decimal places, it is not possible to uniquely identify the files by entering a number.
In order to uniquely identify in such a case, you would need to enter a string.
There are other difficulties with using decimal values. Decimal values cannot be exactly represented as in binary floating point, so it is not possible to check an entered number to figure out the "last" non-zero decimal digit.
If you had a fixed length (number of decimal places) of no more than about 13 decimal places then this could be worked around.
Meanwhile... use strings.
number_as_string = input('File number?', 's');
filename = [number_as_string 'p.txt'];
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!