Searching for a matched string within nested cell and delivering the index

9 Ansichten (letzte 30 Tage)
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:
  1. 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}).
  2. 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.

Akzeptierte Antwort

per isakson
per isakson am 8 Apr. 2021
Bearbeitet: per isakson am 8 Apr. 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 Kommentare
Nazar Adamchuk
Nazar Adamchuk am 14 Apr. 2021
Hello, there is some problem with the function while processing the values variable (I have attached it as values.mat).
I receive the error:
Non-scalar in Uniform output, at index 1, output 1. Set 'UniformOutput' to false.
Then I follow the advise from Matlab and add the 'UniformOutput, false and your solution looks like that:
is = cellfun( @(cac) strcmp(cac{1},'TOTAL'), 'uniformoutput',false );
Unfortunately, after rerunning the code to process values variable (I have attached it as values1.mat) which has different content from values.mat:
Error using cellfun
Input #2 expected to be a cell array, was char instead.
Can you help be find the solution which will pocess values.mat and values1.mat? Thanks!
Nazar Adamchuk
Nazar Adamchuk am 15 Apr. 2021
I call back my previous comment! Everything fine after I added 'uniformoutput',false

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by