searching an array for a value embedded in a string

9 次查看(过去 30 天)
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)?


Kevin Phung
Kevin Phung 2019-1-29
编辑:Kevin Phung 2019-1-29
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.
  10 个评论
Stephen 2019-1-30
Wait again...FINALLY solved this mess myself. Primie example of 100 monkeys given infinite time on computers could generate a Shakespearean sonnet!
[FileName,PathName] = uigetfile('*.txt','Select the txt file'); % select or enter the name of the txt file containing the ImageJ metadata
path=strcat(PathName,FileName); % construct a path for finding this txt file
opts = detectImportOptions(path); % list the options available in this file
opts.SelectedVariableNames = {'Var1','Var2'}; % choose to import only the columns named 'Var1' and 'Var2'
T = readtable(path,opts); % this imports the metadata into table T
C = table2cell(T); % convert table T to array C
search = 'Time Per Frame'; % define the variable, 'search' to = 'Time Per Frame'
a = C{contains(C(:,1),search)}; % C is your cell array; this locates index and access cell.
Index = find(contains(C(:,1),search)); % finds the row containing the string 'Time Per Frame'
val=C(Index,2); % creates the variable, val, = time per frame (number in the second column of that row)
scantime = cell2mat(val)/1000000 % generates variable, 'scantime', equal to value of time per frame (which is in microsec), in sec
Probably kludgey, but works.
Kevin Phung
Kevin Phung 2019-1-30
woohoo!! You can probably condense a line or two, but that's not too important.
now I can finally sleep at night.


更多回答(1 个)

madhan ravi
madhan ravi 2019-1-29
编辑:madhan ravi 2019-1-29
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}',...
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
  1 个评论
Stephen 2019-1-30
Thanks, Ravi. Being a newbie, I'm having a little easier time understanding and following Kevin's script. Nevertheless, I appreciate your help.



Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息


Community Treasure Hunt

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

Start Hunting!

Translated by