search text in arraycell

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

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

load matlab_data
whos
Name Size Bytes Class Attributes ans 1x39 78 char data 2488x1 484790 cell
data(contains(data,"WindowCount = '1'"))
ans = 1×1 cell array
{'→WindowCount = '1''}
Then what's wrong with the above?
I don't know the character for the nonprinting arrow so did the contains. Using it would allow for startsWith and if it were possible that there are others outside the range then finding it first or subsetting the range would take care of that.
double(ans{1}(1))
ans = 9
Oh. It's a tab...
tab=string(char(9));
data(startsWith(data,tab+"WindowCount = '1'"))
ans = 1×1 cell array
{'→WindowCount = '1''}
That depends on what the starting data are and what is to be extracted.
My approach is a bit more general (and by that I hope robust as well), in that it finds the constraint indices and then searches for a 'WindowCount = ' expression instance that fits those constraints. It then extracts the assoociated number.
It doesn't assume anything else.
shamal
shamal on 9 Jun 2025
Edited: shamal on 9 Jun 2025
thank you but i've two question
1)
matlab write :
"STRMATCH is not recommended..Use STRNCMP.."
is equal to do this:
idx1 = cellfun(@(x)strcmp(x,'[Wsp]'), data, Unif=0);
idx(1) = find(cellfun(@(x)x==1, idx1));
it's correct?
2)
WCmatch = regexp(data(WCidx(Outidx)), '\d', 'match');
if str is " {'→WindowCount = '16''}"
it give me 1 and 6
but the number is '16'and not 1 and 6
it's possibile to have the full number (16)?
thank you
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.
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)

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]"
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

Community Treasure Hunt

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

Start Hunting!