Input a range of numbers with strings as well

5 vues (au cours des 30 derniers jours)
k5jyw153vcxb
k5jyw153vcxb le 9 Oct 2015
Commenté : k5jyw153vcxb le 16 Oct 2015
Here is what I use right now:
m = input('Number? ');
for ind = length(m):-1:1
filename = ['Data#' num2str(m(ind)) '.txt'];
Output Data{ind}= importfile(filename);
end
And this serves my purpose to input a range of numbers like [1:5] or [2,6,3,5]. But now I would like to be able to also use strings in the input along with the range such as [1:3,2R, 4 Word, 10] in order to input these text files: Data#1.txt, Data#2.txt, Data#3.txt, Data#2R.txt, Data#4 Word.txt, and Data#10.txt I know that cells allow me to have mixed arrays but I cannot figure out away to allow the input of 1:3 to be {1, 2, 3}. Can anyone help me achieve what I am trying to do?

Réponse acceptée

Matt J
Matt J le 9 Oct 2015
You could use myParser below to get the relevant inputs in string form. For example
>> m=myParser('1:3 2R 4 Word 10')
m =
'1' '2' '3' '2R' '4' 'Word' '10'
and now you can do things like
filename = ['Data#' m{i} '.txt'];
simlar to what you had before.
function m=myParser(str)
C=textscan(str,'%s');
C=C{1};
for i=1:length(C)
c=str2num(C{i});
if ~isempty(c)
C{i}=num2cell(c);
end
end
m=[C{:}];
for i=1:length(m)
if isnumeric(m{i})
m{i}=num2str(m{i});
end
end
end
  1 commentaire
k5jyw153vcxb
k5jyw153vcxb le 16 Oct 2015
Yes, this has done exactly what I wanted. Thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by