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
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 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by