searching an array for a value embedded in a string
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Stephen
am 29 Jan. 2019
Kommentiert: Kevin Phung
am 30 Jan. 2019
Probably this is hopelessly simple to solve, but I haven't found the way (being a newbie)
I have a number of large arrays that look like this (a small portion only shown):
I'm trying to find a way to search for and extract the "Pan Scale" value from each array (here, = 2 on line 123, red arrow). This element (Pan Scale) may not always be on line 123, so I can't just search for the 123rd row in all the arrays.
How do I search for the string "Pan Scale" and then how do I extract its value (the number after the equal sign)?
UPDATE: OK, I just found a way to get to the cell in my array (named "C"):
C(strcmpi(C(:,1),'Pan Scale = 2'));
but now, how to extract the value (i.e. here, 2)?
0 Kommentare
Akzeptierte Antwort
Kevin Phung
am 29 Jan. 2019
Bearbeitet: Kevin Phung
am 29 Jan. 2019
search = 'Pan Scale';
a = C{contains(C,search)} % if C is your cell array, locate index and access cell.
exp = '\d*'; %all consecutive numeric digits
val = str2num(cell2mat(regexp(a,exp,'match')))
^ you can use the above for your other elements too, like 'LightControl' or 'PinHoleDiameter,' etc.
documentation:
https://www.mathworks.com/help/matlab/ref/regexp.html
10 Kommentare
Kevin Phung
am 30 Jan. 2019
woohoo!! You can probably condense a line or two, but that's not too important.
now I can finally sleep at night.
Weitere Antworten (1)
madhan ravi
am 29 Jan. 2019
Bearbeitet: madhan ravi
am 29 Jan. 2019
The below code can extract the numeric digit/s equated to Pan Scale:
r=cellfun(@(x)regexp(x,'(?<=Pan Scale.{0,10})\d{0,10}\.?\d{0,10}',...
'match'),C,'un',0);
R=cellfun(@str2double,r,'un',0);
Result=vertcat(R{:}) % if you want the result to be a column vector
Result=[R{:}] % if you want the result to be a row vector
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!