search text in arraycell
20 views (last 30 days)
Show older comments
shamal
on 9 Jun 2025 at 13:44
Commented: Star Strider
on 9 Jun 2025 at 18:51
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
0 Comments
Accepted Answer
Star Strider
on 9 Jun 2025 at 15:21
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')
data = LD.data
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
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'
data(WCidx(Outidx)) % Explanation -- The 'regexp' Call Analyses This String And Returns The Number
.
5 Comments
dpb
on 9 Jun 2025 at 18:48
Edited: dpb
on 9 Jun 2025 at 18:49
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
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
on 9 Jun 2025 at 18:51
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')
WCnr = str2double(WCmatch)
More Answers (1)
dpb
on 9 Jun 2025 at 13:56
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!