Borrar filtros
Borrar filtros

stored character string to character matrix by using loop

1 visualización (últimos 30 días)
jarvan
jarvan el 13 de Nov. de 2014
Comentada: jarvan el 15 de Nov. de 2014
I am going to use loop to prompt out 4 course name as 5-character string as form'AB101'. And I need to stored those string in a character matrix course.
n=4;
for i = 1:n
class{i} = input('Enter the course name : ','s')
mymat = [ 'myDataFile' str2num(class{:}) '.mat' ];
save(mymat);
end
str2num('class')
Here, I saved those 4 course by 4 mat file, however, how can I store in one file and switch them to a character matrix?

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 14 de Nov. de 2014
Jarvan - you should avoid using class as a variable name since it conflicts with a MATLAB built-in function of the same name. Consider renaming it to * courseNames*. If you want to save all data to a single file, then try the following
n=4;
courseNames = cell(n,1); % pre-size classNames as a row vector
for k=1:n
courseNames{k} = input('Enter the course name : ','s');
end
% convert courseNames to a character matrix
courseNames = char(courseNames);
% write this variable to file
save('myCourseNames.mat','courseNames');
So we save all 4 course names to the same mat file.
With your code, the str2num(class{:}) probably generated an error (or at least it did for me on the second iteration of your loop). Converting a string that has characters to a number, for example str2num('AB101') would produce an empty matrix. And then, on the second iteration of the loop converting both column elements from class{:} would throw the
Error using str2num
Too many input arguments.
What were you attempting with this line of code?
  2 comentarios
jarvan
jarvan el 15 de Nov. de 2014
I am trying to switch the stored character string to matrix
jarvan
jarvan el 15 de Nov. de 2014
btw your code works for me ,thanks:)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by