How do i make up a word by attaching several words in Matlab?

1 view (last 30 days)
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

Stephen23
Stephen23 on 26 Jan 2016
Edited: Stephen23 on 26 Apr 2021
Easiest Solution: Use dir
According to your other question on this topic you are reading files in a folder. Then the easiest way is to simply read those files using dir, exactly as you were told in your earlier question:
S = dir(...);
S = natsortfiles(S,'\d+\.?\d*'); % optional, see below.
for k = 1:numel(S)
F = S(k).name % filename
val = sscanf(F,'%f') % numeric value
...
end
If you want the files in alphanumeric order, then you can download my FEX submission natsortfiles .
If those p characters indicate "pico", then you can convert the strings to numeric using my FEX submission sip2num:
>> sip2num('5.543p.txt')
ans =
5.5430e-12
Convert Numeric to String
If you really want to continue along the path of defining the filenames from numeric values, then you can use sprintf:
>> V = [1,2.2,3.7,10.09,5.543];
>> N = arrayfun(@(n)sprintf('%#.1fp.txt',n),V,'UniformOutput',false);
>> N{:}
ans = 1.0p.txt
ans = 2.2p.txt
ans = 3.7p.txt
ans = 10.1p.txt
ans = 5.5p.txt
However because some of your filenames have trailing zeros (e.g. '1.0 p.txt'), and the number of decimal digits differs (one, two, or three) you will not find one single format string that will convert all of these values to the strings that you need. You would then have to do some complicated pre-selection and dynamic changing of the format string.
The simpler solution would be to use dir, as you have been told repeatedly.

More Answers (1)

Walter Roberson
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!