Borrar filtros
Borrar filtros

Extract a range of a given cell array

1 visualización (últimos 30 días)
Ole Hansen
Ole Hansen el 8 de Jun. de 2012
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) ?

Respuesta aceptada

Thomas
Thomas el 12 de Jun. de 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 comentarios
Ole Hansen
Ole Hansen el 12 de Jun. de 2012
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 el 12 de Jun. de 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)

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by