フィルターのクリア

Extract a range of a given cell array

1 回表示 (過去 30 日間)
Ole Hansen
Ole Hansen 2012 年 6 月 8 日
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) ?

採用された回答

Thomas
Thomas 2012 年 6 月 12 日
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 件のコメント
Ole Hansen
Ole Hansen 2012 年 6 月 12 日
Hi
Thank you very much for the reply. The GUI must, however, be able to interpret ranges as given in the original poster with case a and b. That is, if I would like to enter 0%-30% then I should enter '30' and not both '0' and '30' using two different prompts. In the case of a midrange, e.g. 25%-55%, then I should enter '25-55'. Note that I use the minus sign inbetween (any other symbol, for instance a comma, will also work - but I should be able to do both case a and b using only one prompt in the inputdlg-function). Case c can be neglected, but case a and b are necessary.
Is that possible?
Thomas
Thomas 2012 年 6 月 12 日
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)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePulsed Waveforms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by