How can I analyse multiple variables with similar names after importing them in a loop?
5 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Matt J
2015 年 7 月 14 日
編集済み: Matt J
2015 年 7 月 14 日
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 件のコメント
Steven Lord
2015 年 7 月 15 日
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.
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!