Sandwiching "num2str" in between file-naming loop.

조회 수: 7 (최근 30일)
Kieran Fung
Kieran Fung 2020년 10월 9일
댓글: Walter Roberson 2020년 10월 9일
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
Walter Roberson
Walter Roberson 2020년 10월 9일
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
Walter Roberson 2020년 10월 9일
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
Kieran Fung
Kieran Fung 2020년 10월 9일
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 2020년 10월 9일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by