MATLAB: Sandwiching “num2str” in between file-naming loop.

file namingnum2str

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

Best Answer

  • 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.