Extracting specific rows from multiple data files to store as variables
13 次查看(过去 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!