Error using save: Argument must contain a character vector

5 views (last 30 days)
Can somebody give me some insight on how to fix this? I am super new to MatLab.
load Assignment_4_data
ones_row = ones(1,8);
Q1_1 = [ones_row;Num1;ones_row];
ones_column = ones(10,1);
Q1_2 = [-ones_column, Q1_1, ones_column];
new_row = Q1_2(7,2:9)-2;
Q1_2(7,2:9)= new_row;
data = Q1_2.^2;
save(part_1,'data');

Accepted Answer

Cam Salzberger
Cam Salzberger on 24 Oct 2017
Edited: Cam Salzberger on 24 Oct 2017
Hello Marki,
I assume that you are trying to save your data variable to a MAT file entitled part_1.mat?
In that case, you just need to add single-quotes around part_1 to make it a character vector. Currently, MATLAB is trying to access a variable called part_1, which seems to exist (maybe) but doesn't contain character data.
save('part_1', 'data')
or just use command syntax:
save part_1 data
Also, unless your assignment specifies otherwise, you can just do:
Q1_2(7,2:9) = Q1_2(7,2:9)-2;
No need to make a temporary variable.
-Cam
  3 Comments
Sara Salimi
Sara Salimi on 8 Apr 2019
What if it is a variable containg a text? I have different directory and seed to save for each them separately in a for loop. I am facing with the same error.
fname=strcat(dirnames(i),'.mat');
save(fname,'X_features','Y_label');
Cam Salzberger
Cam Salzberger on 8 Apr 2019
Edited: Cam Salzberger on 8 Apr 2019
Sara, that should work fine. Though if dirnames is a cell array of character vectors, you'll need to use dirnames{i}. If it's a string array, then it's correct as is.
Can you show what happens when you do this:
class(dirnames)
size(dirnames)
disp(dirnames)
Maybe at some point, dirnames(i) does not contain text? You could always run this code to go into debug mode when your error is hit, and see what the issue is:
dbstop if error
-Cam

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB 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!