Error using "Save" Command.
8 views (last 30 days)
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);
dataA = ConcatenatedSignal(flag:1:nos+flag-1);
save(['C:\Users\AKRA\Desktop\New folder\' names(j),'\channel' num2str(i),'/M_' num2str(k) '.mat'],'dataA');
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.