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 visualización (últimos 30 días)
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];

Respuesta aceptada

Stephen23
Stephen23 el 11 de Jun. de 2021
Editada: Stephen23 el 11 de Jun. de 2021
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:

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by