필터 지우기
필터 지우기

stored character string to character matrix by using loop

조회 수: 1 (최근 30일)
jarvan
jarvan 2014년 11월 13일
댓글: jarvan 2014년 11월 15일
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?

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 11월 14일
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
jarvan
jarvan 2014년 11월 15일
I am trying to switch the stored character string to matrix
jarvan
jarvan 2014년 11월 15일
btw your code works for me ,thanks:)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by