フィルターのクリア

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 Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by