Error using "Save" Command.

9 views (last 30 days)
Hassan Ashraf
Hassan Ashraf on 28 Jun 2019
Commented: Hassan Ashraf on 28 Jun 2019
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

Accepted Answer

Steven Lord
Steven Lord on 28 Jun 2019
Edited: Steven Lord on 28 Jun 2019
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.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!