Searching for a matched string within nested cell and delivering the index
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a nested cell array which is called values.
In this example, there are 5 sub cells. Usually this number is variable from 1 to 30.
Each cell has two subsub cells:
- the first one, or values{5,1}{1,1}, has always only one char value (in this example, TOTAL or MGS)
- the second one, or values{5,1}{2,1}, has matrix consisting from tow columns: on the left - tempearature values, on the right - physical properties.
My goal is twofold:
- to find the sub cell containing the char value 'TOTAL' (values{5,1}) and somehow to get the index of the matrix (the output would be values{5,1}{2,1}).
- to get from the matrix the physical property at 298,15. If the value for room temperature is missing, take the closest value to the RT (the output would be 7 or 6 (if the value at room temperature is missing))
To adress the 1st problem, I have written my handmade solution. This code works if there is in the char TOTAL in a{5,1}{1,1}. However, this string could be elsewhere. In this case, I have a problem.
To find a solution for the 2nd question, I created following script, which from my point of view could be simplier to read.
for m = 1:numel(values)
for n = 1:numel(values(m))
a = values(m);
if string(a{5,1}{1,1}) == 'TOTAL'
k = a{5,1}{2,1}(:,1); %column with temp numbers
n = 298.15; %RT
[~,~,idx] = unique(round(abs(k-n)));
minVal = k(idx==1);
valueAtRT = sprintf('%f', a{1,1}{5,1}(length(a{1,1}{5,1})+idx));
break;
end
end
end
How can I do this?
Thanks for any help.
댓글 수: 2
Stephen23
2021년 4월 8일
@Nazar Adamchuk: please upload sample data in a mat file by clicking the paperclip button.
채택된 답변
per isakson
2021년 4월 8일
편집: per isakson
2021년 4월 8일
Assume there is ONE cell contaning 'TOTAL'
Try
%%
ix = find( cellfun( @(cac) strcmp(cac{1},'TOTAL'), values ) );
num = values{ix}{2};
fys = interp1( num(:,1), num(:,2), 298.15 );
The value of fys is
>> fys
fys =
7.0931
Better
%%
is = cellfun( @(cac) strcmp(cac{1},'TOTAL'), values );
num = values{is}{2};
fys = interp1( num(:,1), num(:,2), 298.15 );
댓글 수: 6
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!