Extract a range of a given cell array
5 views (last 30 days)
Show older comments
I would like, trough a GUI, e.g.:
prompt={'Insert range'}
defans={'Insert range'}
fields = {'num'}
info = inputdlg(prompt, 'Insert handrange', 1, defans)
if ~isempty(info)
info = cell2struct(info,fields)
mynum = str2num(info.num)
end
to extract a range of a given cell array, say:
cellarray={'A','B','C','D','E','F','G','H','I','J','K','L'};
in three different ways:
a) 30 [i.e. 0-30%]
b) 3-7 [i.e. 3-7%]
c) 40- [i.e. 40-100%]
Note that I would like to use a symbol (in this case "-" to denote case c).
How do I make Matlab choose 0-30% using '30' and so forth for the cases b) and c) ?
0 Comments
Accepted Answer
Thomas
on 12 Jun 2012
cellarray={'A','B','C','D','E','F','G','H','I','J','K','L'};
prompt1={'Insert start in %'};
prompt2={'Insert End in %'};
defans1={'Start Value'};
defans2={'End Value'};
fields = {'num'};
info1 = inputdlg(prompt1, 'Input start', 1, defans1);
if ~isempty(info1)
info1 = cell2struct(info1,fields);
mynum1 = str2num(info1.num) ;
end
info2 = inputdlg(prompt2, 'Input end', 1, defans);
if ~isempty(info2)
info2 = cell2struct(info2,fields);
mynum2 = str2num(info2.num) ;
end
len_array=length(cellarray);
start_val=round(mynum1*len_array/100);
if start_val==0
start_val=1;
end
end_val=round(mynum2*len_array/100);
output=cellarray(start_val:end_val)
3 Comments
Thomas
on 12 Jun 2012
cellarray={'A','B','C','D','E','F','G','H','I','J','K','L'};
prompt1={'Insert range in % Eg 1-30'};
defans1={'Start Value'};
fields = {'num'};
info1 = inputdlg(prompt1, 'Input range', 1, defans1);
pos=cell2mat(strfind(info1,'-'));
a=cell2mat(info1);
start_val=str2num(a(1:pos-1));
end_val=str2num(a(pos+1:end));
len_array=length(cellarray);
start_val=round(start_val*len_array/100);
if start_val==0
start_val=1;
end
end_val=round(end_val*len_array/100);
output=cellarray(start_val:end_val)
More Answers (0)
See Also
Categories
Find more on Pulsed Waveforms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!