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!