How can I analyse multiple variables with similar names after importing them in a loop?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Amir Dehsarvi
el 14 de Jul. de 2015
I have a dataset of 246 text files and I can easily load them in MATLAB as 246 variables with exactly the same names as the text files.
files = dir('.../controls/*.txt');
for k = 1:length(files)
load(fullfile('/controls/', files(k).name), '-ascii');
end
The problem is that now I have to modify the variables and work on specific columns. But, unfortunately, I cannot do this in a loop as I do not have access to the names of the variables after loading them.
For example, I want to modify the second column:
L_C9_16(:,2) = L_C9_16(:,2) * 20.32;
And this has to be done to all the variables that have been loaded.
Is there anyway I can simply work on all the variables that are now loaded in a loop? I am not sure if it helps, but the names of the variables are quite similar as they all start with L_C and then it changes according to each subject.
Many thanks in advance :)
0 comentarios
Respuesta aceptada
Matt J
el 14 de Jul. de 2015
Editada: Matt J
el 14 de Jul. de 2015
You should always call load() with an output argument. In this case, it allows you to put the file contents into something indexable, like a cell array.
for k = 1:length(files)
S{k} = load(fullfile('/controls/', files(k).name), '-ascii');
end
and now you can do column-wise manipulations as follows,
S{k}(:,2) = S{k}(:,2) * 20.32;
4 comentarios
Steven Lord
el 15 de Jul. de 2015
The extension (or more specifically, the period separating it from the first part of the file name) is what makes that an invalid field name. You can use FILEPARTS to remove the extension.
If there's a chance your filenames contain characters other than letters, numbers, and underscores you may want to pass them through matlab.lang.makeValidName, matlab.lang.makeUniqueStrings, or GENVARNAME before trying to use them as field names.
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!