search text in arraycell

조회 수: 3 (최근 30일)
shamal
shamal 2025년 6월 9일
댓글: Star Strider 2025년 6월 9일
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

채택된 답변

Star Strider
Star Strider 2025년 6월 9일
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
dpb
dpb 2025년 6월 9일
편집: dpb 2025년 6월 9일
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 2025년 6월 9일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2025년 6월 9일
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
shamal
shamal 2025년 6월 9일
편집: shamal 2025년 6월 9일
i WANT THIS...After " [Wsp]" and before "[Wsp\DetachWindows]"
shamal
shamal 2025년 6월 9일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by