Sandwiching "num2str" in between file-naming loop.

4 views (last 30 days)
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);
Stress = readtable(myfilename(i));
end
  1 Comment
Walter Roberson
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'

Sign in to comment.

Accepted Answer

Walter Roberson
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));
Stress = readtable(filename);
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 Comments
Kieran Fung
Kieran Fung on 9 Oct 2020
Thanks for the help Walter :)
Going through the Mathworks examples didn't quite capture what I was after but you've exposed me too a cool new possibility when comning strings. Thanks!
One question: Why did we not have to use any float operators here when summing < A + NoNodes(i) + B> here?
Walter Roberson
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});
Stress = readtable(filename);
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Stress and Strain 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!