Error using save: Argument must contain a character vector

조회 수: 4 (최근 30일)
Marki Wong
Marki Wong 2017년 10월 24일
편집: Cam Salzberger 2019년 4월 8일
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');

채택된 답변

Cam Salzberger
Cam Salzberger 2017년 10월 24일
편집: Cam Salzberger 2017년 10월 24일
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
Sara Salimi
Sara Salimi 2019년 4월 8일
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 2019년 4월 8일
편집: Cam Salzberger 2019년 4월 8일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by