Skipping files in a set

2 ビュー (過去 30 日間)
MR0d
MR0d 2019 年 11 月 27 日
コメント済み: MR0d 2019 年 12 月 2 日
I have 600 .vec files from a recent experiment and I need to remove/skip certain files from this group because they contain bad data. Say I need to remove files 132, 290, and 404. How would I do this? Here is the code I have for creating the matrices I need. Thank you.
rightfiles=dir('*.vec'); % Selects only the files in the directory ending with .vec. Generates a 600x1 struct.
for z=1:1:size(rightfiles)
f=importdata([rightfiles(z).name]);%Transitions data from .vec to .mat
%Sets x,y,u,v into matrices of only columns 1-4 from rightfiles, respectively
x=f.data(:,1);
y=f.data(:,2);
u=f.data(:,3);
v=f.data(:,4);
%Reshapes x,y,u,v into IxJ matrices
xx=reshape(x,[I,J])';
yy=reshape(y,[I,J])';
uu=reshape(u,[I,J])';
vv=reshape(v,[I,J])';
%Convert to the correct units
ConversionU=uu.*(m_pix/dt);
ConversionV=vv.*(m_pix/dt);
ConversionX=xx*m_pix;
ConversionY=yy*m_pix;
%Stack the vec files
U(:,:,z)=ConversionU;
V(:,:,z)=ConversionV;
X(:,:,z)=ConversionX;
Y(:,:,z)=ConversionY;
end
  2 件のコメント
KSSV
KSSV 2019 年 11 月 28 日
How you will decide the files have bad data?
MR0d
MR0d 2019 年 12 月 2 日
I have already gone through the data and selected the files with bad data.

サインインしてコメントする。

回答 (2 件)

Jan
Jan 2019 年 11 月 28 日
編集済み: Jan 2019 年 11 月 28 日
What does "files 132, 290, and 404" mean? Are these the indices in rightfiles, or are these parts of the file name?
rightfiles=dir('*.vec');
rightfiles([132, 290, 404]) = [];
for z = 1:numel(rightfiles)
f = importdata(rightfiles(z).name);
...
I've added some changes:
  • size(rightfield) replies a vector. 1:1:vector might reply something other than you expect. Although this works here for accident, it is safer to use numel instead.
  • 1:x looks nicer than 1:1:x
  • No need for square brackets around rightfiles(z).name. The [ and ] are the operator for concatenation. With one input only, there is nothing to concatenate.
Apply a pre-allocation before the loop. Letting arrays grow iteratively is extremely expensive.
  1 件のコメント
MR0d
MR0d 2019 年 12 月 2 日
The random numberes I selected were just examples. They are random indices of rightfiles that I would like to exclude. Thank you for the advice. And I do have lines for pre-allocation, just didn't think it was important for my question so I did not include them. Thank you very much.

サインインしてコメントする。


Image Analyst
Image Analyst 2019 年 11 月 28 日
Make a function calls HasGoodData() that take in f and determines if the data is bad or not and returns true for good data and false for bad data. Then, in your loop...
for z = 1 : size(rightfiles)
f=importdata([rightfiles(z).name]); % Transitions data from .vec to .mat
if ~HasGoodData(f) % If not good data...
continue; % Skip this file by jumping to the very bottom of the loop but continuing with the next iteration.
end
% Run code for good data...
end % of for loop

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by