Extracting specific rows from multiple data files to store as variables
11 ビュー (過去 30 日間)
古いコメントを表示
I'm very new to Matlab.
I have multiple data files (file type file) of dimensions 5x12001, each representing data from an experiment.
For each file I want to extract the first two rows (i.e. to have a 2x12001 matrix from each file (or two 1x matrices)) and store in such a way that allows me to keep track of which stems from which file.
I've tried generating a cell array:
files = dir('*.');
files = files(~ismember({files.name},{'.', '..'})); %gets rid of empty files invisible in explorer
for i = 1:length(files)
C{i} = load(files(i).name);
end
But I'm not sure how to efficiently manipulate the matrices once indexed in a cell array.
This crappy function sort of represents what I'm trying to achieve:
function [wavelength, datavalue] = getmatrix(filename)
matrix = readmatrix(filename);
wavelength = matrix(1,:);
datavalue = matrix(2,:);
end
But I'd like to be able to loop through all the files without having to manually call the function on each filename.
Thanks, and sorry for my staggering ineptitude.
0 件のコメント
採用された回答
Stephen23
2019 年 6 月 17 日
編集済み: Stephen23
2019 年 6 月 17 日
"....and store in such a way that allows me to keep track of which stems from which file."
That is easy using the structure returned by dir:
S = dir('*.csv'); % better to specify the file extension!
for k = 1:numel(S)
tmp = load(S(k).name); % you should probably use DLMREAD or similar.
S(k).data = tmp(1:2,:) % 1st & 2nd rows.
% OR
S(k).wavelength = tmp(1,:); % 1st row.
S(k).datavalue = tmp(2,:); % 2nd row.
end
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Workspace Variables and MAT Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!