MATLAB Answers

"Error using save Must be a string scalar or character vector." - but it is a string!

1.258 views (last 30 days)
EQ on 7 Apr 2019
Commented: EQ on 8 Apr 2019
I'm getting the error when I run the following line:
where n is my loop index. This is bizarre. For the record, when I stop at the breakpoint, typing this at the command line works:
save data/test1.mat U U_old dt
But I need to be able to change the filename within the loop, hence why I'm doing it the first way. When I type this at the command line:
I get the expected 'data/test1.mat' result. The variable U is an 889x4 matrix, U_old is the same size, and dt is a single float. Even typing
at the command line when I stop gives me this error. What gives?


Sign in to comment.

Accepted Answer

A. Sawas
A. Sawas on 7 Apr 2019
Edited: A. Sawas on 7 Apr 2019
You need to enclose the variable names with qoutations like this:

  1 Comment

EQ on 8 Apr 2019
doh! I feel stupid now, this isn't my first time using the save command. Thanks!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 7 Apr 2019
save( sprintf('data/test%d.mat', n), 'U', 'U_old', 'dt');
The quotes around the U and so on are the important part for your problem; the sprintf is just a cleaner way of creating the file name.


Sign in to comment.


Community Treasure Hunt

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

Start Hunting!