# Sandwiching "num2str" in between file-naming loop.

4 views (last 30 days)
Kieran Fung on 9 Oct 2020
Commented: Walter Roberson on 9 Oct 2020
I'm trying to create an automated loop to extract data from .txt files that are generated from a different script. <NoNodes> is describing for a variable such that NoNodes = [16, 36, 64, 100, 144 . . . (n-2)^2, n^2]. Just squaring even integers starting at 4.
The real issue I am having is solving for <myfilename(i)>. I've put a code break at line that defines <Stress> so I can see the affects. It combines strings in a way that I do not understand and generates numbers I do not know how they've been solved.
I.e. for i = 1, this is the string output I got:
ans =
"CombinedStresses.Periodic.N49CombinedStresses.Periodic.N54"
Any idea's for the code below?
N = 100;
NoNodes = (4:2:N).^2
A = "CombinedStresses.Periodic.N%u";
B = ".Z5.Gaussian.Data.10_08_2020.Trial.1.txt";
for i = 1:length(NoNodes);
myfilename(i) = sprintf(A, num2str(NoNodes(i)), B);
filename = fullfile('D:\University\Fall 2020\Class\HW3\Raw Data\Stresses', myfilename);
end
Walter Roberson on 9 Oct 2020
no it doesn't. The num2str gives '16' which is char(49 54) and the %u converts the char(49) to '49'. The vector is not finished outputting so it repeats the format and converts the char(54) to '54'. So you end up with like 'PQR49PQR54'

Walter Roberson on 9 Oct 2020
N = 100;
NoNodes = (4:2:N).^2
A = "CombinedStresses.Periodic.N";
B = ".Z5.Gaussian.Data.10_08_2020.Trial.1.txt";
for i = 1:length(NoNodes);
myfilename(i) = A + NoNodes(i) + B;
filename = fullfile('D:\University\Fall 2020\Class\HW3\Raw Data\Stresses', myfilename(i));
end
The problem you had was you have a %u format element in your format, which was the content of A, but you were passing in the corresponding position the content of num2str(16) . The result of num2str(16) is '16' which is char([49 54]) . The character codes were converted through the %u format item.
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 9 Oct 2020
A quite valid alternative for you would have been
N = 100;
NoNodes = (4:2:N).^2
A = "CombinedStresses.Periodic.N";
B = ".Z5.Gaussian.Data.10_08_2020.Trial.1.txt";
for i = 1:length(NoNodes);
myfilename{i} = sprintf('%s%u%s', A, NoNodes(i), B);
filename = fullfile('D:\University\Fall 2020\Class\HW3\Raw Data\Stresses', myfilename{i});
end
When you use " around a list of characters, you are defining a string() object, which is distinct from a character vector. string() objects have methods defined for them that are not available for character vectors.
When you have a character vectors P and Q, the P+Q operation is defined like double(P)+double(Q) -- and has the same limitations such as one of them having to be scalar or else the two having to be the same length.
>> 'ABC'+1, char(ans)
ans =
66 67 68
ans =
'BCD'
However, for string() objects P and Q, the P+Q operation is defined to be string concatenation, like
string(strcat(cellstr(P), cellstr(Q)))
and furthermore, if one of P or Q is string and the other is numeric, then the P+Q operation is like
string(strcat(cellstr(string(P)), cellstr(string(Q))))
and string() applied to a numeric value is something like num2str() of the numeric value.
"F" + (1:5)
would construct ["F1" "F2" "F3" "F4" "F5"] which is a string array of size 1 x 5, and indexing at (1) would be "F1" .
There is no float() operation because the operating involved is concatenation not arithmetic.