from C_T_Sep_1 to C_T_Sep_10 they are vectors the same thing for C_A_Sep_1... C_A_Sep_10, I want to create matrix C_T_Sep and C_A_Sep and put the vector inside it, using loop
조회 수: 1 (최근 30일)
이전 댓글 표시
C_T_Sep_1 = importdata('C_T_Sep_1.txt') ;
C_A_Sep_1 = importdata('C_A_Sep_1.txt') ;
C_T_Sep_2 = importdata('C_T_Sep_2.txt') ;
C_A_Sep_2 = importdata('C_A_Sep_2.txt') ;
C_T_Sep_4 = importdata('C_T_Sep_4.txt') ;
C_A_Sep_4 = importdata('C_A_Sep_4.txt') ;
C_T_Sep_5 = importdata('C_T_Sep_5.txt') ;
C_A_Sep_5 = importdata('C_A_Sep_5.txt') ;
C_T_Sep_10 = importdata('C_T_Sep_10.txt') ;
C_A_Sep_10 = importdata('C_A_Sep_10.txt') ;
C_T_Sep = [C_T_Sep_1;C_T_Sep_2;C_T_Sep_4;C_T_Sep_5;C_T_Sep_10];
C_A_Sep = [C_A_Sep_1;C_A_Sep_2;C_A_Sep_4;C_A_Sep_5;C_A_Sep_10];
채택된 답변
Stephen23
2021년 6월 11일
편집: Stephen23
2021년 6월 11일
Your code design makes your task much harder. By forcing meta-data into the variable names, you have forced yourself into writing slow, inefficient complex, buggy code that is difficult to debug. You make it harder to use a loop, as you request. Repeating code is also a sign that you are doing something wrong: the computer is much faster at repeatedly doing menial simple tasks, so there is no point in doing its job for it by copy-and-pasting like that.
The MATLAB approach is to use arrays and indexing, just as the MATLAB documentation shows:
When you use indexing with one array, then your task is easy:
P = 'absolute or relative path to where the files are saved';
V = [1,2,4,5,10];
N = numel(V);
CT = cell(1,N); % preallocate
CA = cell(1,N); % preallocate
for k = 1:N
FT = sprintf('C_T_Sep_%d.txt',V(k));
FA = sprintf('C_A_Sep_%d.txt',V(k));
CT{k} = importdata(fullfile(P,FT));
CA{k} = importdata(fullfile(P,FA));
end
C_T_Sep = vertcat(CT{:}) % comma-separated list
C_A_Sep = vertcat(CA{:}) % comma-separated list
See also:
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!