Help with extracting info from a listbox in a specific way

8 ビュー (過去 30 日間)
Jason
Jason 2020 年 3 月 18 日
編集済み: BobH 2020 年 3 月 18 日
Hello. I am using a uitable (in APPDesigner) to get user defined positions to move a stage to and take an image. There are 9 rows and 12 columns.
As can be seen, the red cells have been selected and this "list of positions" is populated in a listbox.
As I want to scan in the row direction (shown by black arrow), I want to collect all Row 1 positions, Row 2 positions etc and add to a string that I can then pass to a move routine for the stage.
"row =ALL" means all rows at that column are required, so this is the strings that Im trying to achieve by looping through the list box items, but dont know how to.
i.e. in row 1, all the columns 2,4,5,10,1 need to be scanned, so a string like:
"r1,2,4,5,10,1"
and row 2, the columns 2,4,5,10,1 need to be scanned, so
"r2,2,4,5,10,1"
...and for row 9
"r9,1,7"
This is my attempt
%Get number of items in listbox
items=app.ListBox.Items
nl=numel(items)
s1='r1,'
for i=1:nl
line=(items(i))
%get row
if contains(line, 'ALL')
disp(['Found at line',num2str(i)])
b=regexp(line,'\d+(\.)?(\d+)?','match')
b=b{1}
cl=b{1,1}
cl= convertCharsToStrings(cl)
class(cl)
s1=strjoin(s1,cl)
break
end
end
Or if its easier, an array of string arrays output would be ok
"r1" "2" "4" "5" "10" 1"
"r2" "2" "4" "5" "10" 1"
Thanks for any help
  2 件のコメント
BobH
BobH 2020 年 3 月 18 日
I recommend that you produce consistent-looking lines in the listbox, which will simplify parsing. For example,
  • row=<whatever> always has a comma or not
  • always spell out 'column' or always abbreviate (row 5)
  • always 'exp(ms)' (row 5)
Jason
Jason 2020 年 3 月 18 日
Yes of course, i didnt realise the inconsistency.

サインインしてコメントする。

採用された回答

BobH
BobH 2020 年 3 月 18 日
編集済み: BobH 2020 年 3 月 18 日
You can build a cell array using the row as the index, and each element of the cell array can be a numeric array of the column(s).
Build the output string when all the number-processing is complete
str = { ...
'row=ALL, column=2 exp(ms)=100'; ...
'row=1, column=4 exp(ms)=100'; ...
'row=2, column=4 exp(ms)=100'; ...
'row=3, column=4 exp(ms)=100'; ...
'row=5, column=ALL expms=100'; ...
'row=1, column=10 exp(ms)=100'; ...
'row=2, column=10 exp(ms)=100'; ...
};
nrows = 9;
ncols = 12;
R = cell(nrows,1); % result
% parse all lines of 'str'
toks = regexp(str, 'row=(.*?),\s*column=(.*?)\s+exp', 'tokens', 'once');
for i = 1:length(toks)
% first element has the extracted row,
% second has extracted column
[rn, cn] = toks{i}{:};
if( strcmpi(rn,'ALL') )
rn = 1:nrows;
else
rn = str2double(rn);
end
if( strcmpi(cn,'ALL') )
cn = 1:ncols;
else
cn = str2double(cn);
end
% each element in rn is an index into R
for w = 1:length(rn)
for x = 1:length(cn)
R{ rn(w) } = [ R{ rn(w) } cn(x) ]
end
end
end
% column duplications can occur with 'ALL'
% remove the dups, but keep the original sequence
% (to get a sorted unique sequence, remove 'stable')
R = cellfun(@(X) unique(X,'stable'), R, 'un',0);
% build an output string
for i = 1:nrows
if( ~isempty(R{i}) )
fprintf( 'r%d', i );
fprintf( ',%d', R{i} );
fprintf( '\n' );
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by