How can I create a 2-term, 1-by-1 string using sprintf?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Yasmin Ben Azouz
el 5 de Mayo de 2022
Comentada: Yasmin Ben Azouz
el 9 de Mayo de 2022
I am using the following code to refer to 4000x15 doubles I have in my workspace, they are named P1-17. When I use the code however, the name becomes a 1x2 char and therefore a variable itself. When I try to use it in the function that's in the loop, it does not refer to the 4000x15 data at all.
Does anyone know how to fix this or do it in another way? I have tried a lot of things but I can't figure this one out.
for j = 1:1:17
filename = (sprintf('%s%d','P',j)) ;
[sgmean] = DataPrep(filename) ;
end
3 comentarios
Stephen23
el 9 de Mayo de 2022
"but use one of the alternatives in the documentation you attached?"
You don't need to guess, I already told you which alternative to use: LOAD into an output variable.
Respuesta aceptada
Stephen23
el 9 de Mayo de 2022
Editada: Stephen23
el 9 de Mayo de 2022
Here is a way to import those badly-named variables, assuming exactly one variable per MAT file:
N = 17;
C = cell(1,N);
for kk = 1:N
F = sprintf('P%d',kk);
C(kk) = struct2cell(load(F));
end
Your data are stored in the cell array C, for example the 2nd file:
C{2}
The code would be simpler and more robust if the variable names were exactly the same in every file.
Más respuestas (0)
Ver también
Categorías
Más información sobre Whos en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!