search text in arraycell
7 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
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')
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 件のコメント
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
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
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')
WCnr = str2double(WCmatch)
その他の回答 (1 件)
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...
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!