Help with extracting info from a listbox in a specific way

1 view (last 30 days)
Jason
Jason on 18 Mar 2020
Edited: BobH on 18 Mar 2020
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 Comments

Sign in to comment.

Accepted Answer

BobH
BobH on 18 Mar 2020
Edited: BobH on 18 Mar 2020
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!