When I import this function, there is a error when I run this codes.

조회 수: 5 (최근 30일)
Chris
Chris 2021년 11월 4일
답변: Image Analyst 2021년 11월 4일
function importfile(fileToRead1)
%IMPORTFILE(FILETOREAD1)
% Imports data from the specified file
% FILETOREAD1: file to read
% Auto-generated by MATLAB on 04-Nov-2021 14:13:03
% Import the file
newData1 = load('-mat', fileToRead1);
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
importfile('gravity_anomaly_.mat')
<Error>
Not enough input arguments.
Error in importfile (line 9)
newData1 = load('-mat', fileToRead1);
  댓글 수: 1
DGM
DGM 2021년 11월 4일
It works fine for me.
whos % nothing in the base workspace
importfile('carbig.mat') % load
whos % now there's a bunch of stuff in the workspace
Name Size Bytes Class Attributes Acceleration 406x1 3248 double Cylinders 406x1 3248 double Displacement 406x1 3248 double Horsepower 406x1 3248 double MPG 406x1 3248 double Mfg 406x13 10556 char Model 406x36 29232 char Model_Year 406x1 3248 double Origin 406x7 5684 char Weight 406x1 3248 double cyl4 406x5 4060 char org 406x7 5684 char when 406x5 4060 char
function importfile(fileToRead1)
%IMPORTFILE(FILETOREAD1)
% Imports data from the specified file
% FILETOREAD1: file to read
% Auto-generated by MATLAB on 04-Nov-2021 14:13:03
% Import the file
newData1 = load('-mat', fileToRead1);
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
end

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

채택된 답변

Steven Lord
Steven Lord 2021년 11월 4일
Is this line:
importfile('gravity_anomaly_.mat')
inside the importfile function or do you run this in the Command Window?
I suspect you called importfile with no input arguments. If that's correct, move that line I quoted above out of the importfile function and use that to call importfile with an input argument.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2021년 11월 4일
load() is not documented as being able to accept the -mat option before the file name, so it is possible that in your version (which you did not mention... hint, hint) that the parsing is deciding that the -mat is the first option and that the filename is missing.

Image Analyst
Image Analyst 2021년 11월 4일
You don't need that function. In fact we usually don't recommend using assignin() anyway. Instead of the importfile() function, which reads the filename into the base workspace, you can simply call load() which will do exactly the same thing:
load('gravity_anomaly_.mat')
Again, there is no need to call that importfile() function from your script at all.

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by