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!