search text in arraycell

7 views (last 30 days)
shamal
shamal on 9 Jun 2025
Commented: Star Strider on 9 Jun 2025
hi, i've arraycell and i want catch scalar in "WindowCount" after "[Wsp]" but before "[Wsp\DetachedWindows]"
See pics
Result is =1
i try this :
newStr = extractBetween(data,"[Wsp]","[Wsp\DetachedWindows]")
newStr =
2488×0 empty cell array
But receive empty cell array

Accepted Answer

Star Strider
Star Strider on 9 Jun 2025
This may be more convoluted than it needs to be, however it has the virtue of returning the desired result. I know of no other relatively straightforward way of getting the result you want.
Try this --
imshow(imread('Immagine.png'))
LD = load('matlab_data.mat')
LD = struct with fields:
data: {2488×1 cell}
data = LD.data
data = 2488×1 cell array
{'[Wsp]' } {'→(C2526AC1-535C-4a67-92B5-101E53EA9E4B) = '{0}''} {'→ActiveIndex = '0'' } {'→MaximizedFlag = '0'' } {'→ProductVersion = '14.0.25793.400'' } {'→Version = '1'' } {'→WindowCount = '1'' } {'[Wsp\DetachedWindows]' } {'→WindowCount = '0'' } {'[Wsp\Window_0]' } {'→ContainerHeight = '776'' } {'→ContainerWidth = '1916'' } {'→Flags = '0'' } {'→Height = '542'' } {'→Left = '0'' } {'→MinX = '-1'' } {'→MinY = '-1'' } {'→State = '1'' } {'→Top = '0'' } {'→ViewType = '0'' } {'→Width = '1682'' } {'[Wsp\Window_0\ChartManager]' } {'→ChartCount = '1'' } {'→OFFSeriesCount = '0'' } {'[Wsp\Window_0\ChartManager\Background]' } {'→BackgroundColor = '0'' } {'[Wsp\Window_0\ChartManager\CaptionShow]' } {'→ShowCaption = '1'' } {'[Wsp\Window_0\ChartManager\Chart_0]' } {'→LSeriesNum = '0'' }
idx1 = cellfun(@(x)strmatch(x,'[Wsp]'), data, Unif=0);
idx(1) = find(cellfun(@(x)~isempty(x), idx1));
idx2 = cellfun(@(x) strmatch(x, '[Wsp\DetachedWindows]'), data, Unif=0);
idx(2) = find(cellfun(@(x)~isempty(x), idx2)) % Indices Constraining The Resuls
idx = 1×2
1 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
WCstr = cellfun(@(x)strfind(x,'WindowCount = '), data, Unif=0); % Find The Position Of The Requested String
WCidx = find(cellfun(@(x)~isempty(x), WCstr)); % Associated Indices
Outidx = WCidx > idx(1) & WCidx < idx(2); % Compare To Constraints
WCmatch = regexp(data(WCidx(Outidx)), '\d', 'match'); % Extract Numerical VAlue
WCnr = str2double(WCmatch{:}) % Convert To 'double'
WCnr = 1
data(WCidx(Outidx)) % Explanation -- The 'regexp' Call Analyses This String And Returns The Number
ans = 1×1 cell array
{'→WindowCount = '1''}
.
  5 Comments
dpb
dpb on 9 Jun 2025
Edited: dpb on 9 Jun 2025
That's the problem -- the problem definition isn't that clearly well defined -- agree that if it were possible to have the specific string somewhere besides within the bounding strings given, then it would be needeed to bound the location.
But, the string-finding functions are cellstr aware so don't see the need for cellfun() here...
load matlab_data
S1='[Wsp]';
S2='[Wsp\DetachedWindows]';
ix1=find(matches(data,S1));
ix2=find(matches(data,S2));
D=data(ix1:ix2);
N=extract(D(contains(D,'WindowCount =')),digitsPattern); % the string digit
N=str2double(N{:}) % convert to numeric
N = 1
without @cellstr and is same thing.
I guess maybe I misread the OP's original posting as saying he already knew the answer would be 1 in which case there wasn't any point to the search anyway....I guess perhaps in the application it could be some other number besides 1.
Star Strider
Star Strider on 9 Jun 2025
As always, my pleasure!
With respect to your questions --
1.) Yes. There may be several ways to get that same result.
2.) Change it to:
WCmatch = regexp(data(WCidx(Outidx)), '\d*', 'match');
The '\d*' will match more than one consecutive digit.
Testing that--
str = " {'→WindowCount = '16''}";
WCmatch = regexp(str, '\d*', 'match')
WCmatch = "16"
WCnr = str2double(WCmatch)
WCnr = 16

Sign in to comment.

More Answers (1)

dpb
dpb on 9 Jun 2025
newStr = extractBetween(data,"[Wsp]","[Wsp\DetachedWindows]")
extractBetween searches in a given string, not between members of an array of string/cellstr.
First find the string of interest and then parse it...
ixWindow=startsWith(data,'WindowCount');
WindowCountLines=data(ix);
...
Since there are multiple lines with the given string match and the desired result isn't clearly stated, you'll have then decide how to know which one(s) are those of interest...
  2 Comments
shamal
shamal on 9 Jun 2025
Edited: shamal on 9 Jun 2025
i WANT THIS...After " [Wsp]" and before "[Wsp\DetachWindows]"
shamal
shamal on 9 Jun 2025
alternatively you can extract all the rows that contain "Window Count" and "Input_Ncon" and when I have the cells with these extrapolate the information I need

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!