필터 지우기
필터 지우기

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 CenterFile Exchange에서 Pulsed Waveforms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by