How to extract array subset

I'm trying to extract an array subset falling into the range from a very large array. The range from Low to High were from user input and the array was loaded from a data file. so the range may not be covered in the data file. the data file is ususally very large, so i don't want to use for loop to cycle through the data set. say for example, I want to pull out the subset from WaveLength that is between Low_WL and High_WL. following is just an example. the range could be anythng like, (500, 4000), (3000, 5000) etc. the data set is a 1X10 cell array with C{3} being the wavelength. I want to save all the fields of cells(C{1} to C{10}) with wavelength falling in range selected to a subset .
pseudo code:
%%wavelength range from user input
WL_low = 2000.0;
WL_High = 2200;
%% declare data structure to store data
DS = struct('mol','name','dt',{{1 2 3 4 5 6 7 8 9 10}});
DS(1:39,1)=DS;
%%Number of molecules selected = 4
for k = 1:4
fid = fopen(sprintf('hit_%d.txt',k));
C = textscan(fid,'%1d%1d%f%f%f%f%f%f%f%f');
%% where C{3} is the cell storing the wavelenth data
%% I want to select the data that falls within the wavelength range and
%% store them to structure if it is not empty
C subset = if C{3} is in range
DS(k).dt = C subset;
end
Any suggestions are highly appreciated! Chong

1 个评论

This is not clear, give a short example with expected result

请先登录,再进行评论。

 采纳的回答

WaveLength(WaveLength>=Low_WL & WaveLength<=High_WL)

5 个评论

[Chong commented]
Thank you Azzi, I guess I oversimplied my problem. I'll edit my Q.
Chong, if you want to add a comment, click on comment on this anser.
[Chong commented]
Azzi,
your answer works perfect for the array, C{3}. but how can I apply the selection to other fields in the cell? Thanks,
What do you mean by field?
for example, I got the dataset from opening a file, and store the dataset to a cell, C{1}, C{2}...C{10} ,with C{3} as the wavelength. I want to select data that falls within the wavelength ranges.
fid = fopen(sprintf('hit_%d.txt',k));
C = textscan(fid,'%1d%1d%f%f%f%f%f%f%f%f');
DS(k).dt = C;
Bellow is some real data,
4 1 3373.09372 1.11E-24 0.6199 0.0698 0.084 1871.2232 0.75 -0.003712
4 5 3373.39637 6.41E-25 0.582 0.0722 0.092 264.2666 0.75 -0.003194
4 1 3373.41688 1.57E-23 0.7 0.0693 0.082 1381.2141 0.75 -0.003972
4 3 3373.45625 2.75E-25 8.244 0.0652 0.076 1629.7286 0.75 -0.004793
4 2 3373.49431 5.44E-25 27.61 0.0726 0.092 1551.4128 0.75 -0.003151
4 1 3373.52899 2.12E-23 0.7031 0.0694 0.083 1311.4242 0.75 -0.003885
so
if I set the range as from 3350 to 3373.4. it should return results as:
4 1 3373.09372 1.11E-24 0.6199 0.0698 0.084 1871.2232 0.75 -0.003712
4 5 3373.39637 6.41E-25 0.582 0.0722 0.092 264.2666 0.75 -0.003194
I hope it is clear. Any suggetions? Thanks again.

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by