Searching for a matched string within nested cell and delivering the index
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nazar Adamchuk
le 7 Avr 2021
Commenté : Nazar Adamchuk
le 15 Avr 2021
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 commentaires
Stephen23
le 8 Avr 2021
@Nazar Adamchuk: please upload sample data in a mat file by clicking the paperclip button.
Réponse acceptée
per isakson
le 8 Avr 2021
Modifié(e) : per isakson
le 8 Avr 2021
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Cell Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!