Error using "Save" Command.

조회 수: 14 (최근 30일)
Hassan Ashraf
Hassan Ashraf 2019년 6월 28일
댓글: Hassan Ashraf 2019년 6월 28일
I am trying to save some files iteratively in a loop anf getting below error. Can someone please help me out?
Error: Error using save
Must be a string scalar or character vector.
names=["CH" , "OH" , "EX" , "FL" , "GR" , "IN" , "SU" , "PR" , "RT"];
fields = fieldnames(U);
fields = fields(5:13);
for j=1:nMotions
for i=1:nChannels
CM=U.(fields{j});
CM=CM(:,i);
ConcatenatedSignal=CM';
flag=1;
for k=1:1:floor(length(ConcatenatedSignal)/nos)
dataA = ConcatenatedSignal(flag:1:nos+flag-1);
save(['C:\Users\AKRA\Desktop\New folder\' names(j),'\channel' num2str(i),'/M_' num2str(k) '.mat'],'dataA');
flag=flag+nos;
end
end
end

채택된 답변

Steven Lord
Steven Lord 2019년 6월 28일
편집: Steven Lord 2019년 6월 28일
Your names variable is a string array. This means names(j) is a string. Compare:
c1 = 'abc';
s1 = string(c1);
c2 = [c1 '123']
s2 = [s1 '123']
c2 is a 1-by-6 char vector that save knows how to handle. s2 is a 1-by-2 string array which is not a scalar, so save doesn't know how to handle it. If you want to build a longer (in terms of strlength) string array from a string and a char or from multiple string arrays, combine them with +. You can even add in numbers and they will be converted into a string.
s3 = 'C:\Users\AKRA\Desktop\New folder\' + s1 + '\channel' + ...
5 + '/M_' + 42 + '.mat'
s4 = s1 + 123
This works as long as at least one of the pieces of text and/or numeric data you're trying to combine is a string. If they're all char or numeric data, you'll need to concatenate with square brackets and convert the numbers to text yourself.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by