- scripts do not have inputs or outputs.
- functions must start with the word function.
- they cannot be mixed together in one file.
Error: Attempt to execute script as function/no return for "which" command.
1 view (last 30 days)
Show older comments
Brooke Sarley
on 30 Jan 2016
Commented: Steven Lord
on 30 Jan 2016
This is a very common problem, but thus far I have not been able to fix my error using the Matlab troubleshooting solutions. I'm kind of new to matlab so forgive me for being green if it's something obvious I just don't see. The script file I'm trying to modify is called waxsimport.m:
waxsexposures = waxsimport(waxs_exposures_2015.par, 1, inf)
%%Initialize variables.
delimiter = ' ';
if nargin<=2
startRow = 1;
endRow = inf;
end
%%Read columns of data as strings:
formatSpec = '%s%s%s%s%s ... %s%s%[^\n\r]';
%%Open the text file.
fileID = fopen('waxs_exposures_2015.par', '/r/');
end
(that's not the entire thing) returns:
Attempt to execute SCRIPT waxsimport as a function: C:\Users\Owner\Documents\...\waxsimport.m
Error in waxsimport (line 1) waxsexposures = waxsimport('waxs_exposures_2015.par', 1, inf)
I read through a few q&a's and I have deleted all other files involving the phrase "waxsimport" from the path, so it's not looking for the wrong script.
Also, I tried the command
>> which -all ‹waxsimport›
'‹waxsimport›' not found.
>> which -all ‹waxsimport.m›
'‹waxsimport.m›' not found.
So I double checked and waxsimport.m was in the path. The goal is to import data from a .par into a matrix in the workspace.
1 Comment
Stephen23
on 30 Jan 2016
These tutorials are a good way to learn basic MATLAB usage:
Accepted Answer
Walter Roberson
on 30 Jan 2016
No, the file needs to start with
function waxsexposures = waxsimport(waxs_exposures_2015, startRow, endRow)
but you need to invoke it from outside the function, and the invocation in that other routine would look like
waxsexposures = waxsimport('waxs_exposures_2015.par', 1, inf)
Or is your intent to provide "default" values if the user did not specify any inputs? If that was what you were trying to do then you would use a different form: you would use
function waxsexposures = waxsimport(waxs_exposures_2015, startRow, endRow)
if nargin < 1
waxs_exposures_2015 = 'waxs_exposures_2015.par';
end
if nargin < 2
startRow = 1;
end
if nargin < 3
endRow = inf;
end
delimiter = ' ';
%%Read columns of data as strings:
formatSpec = '%s%s%s%s%s ... %s%s%[^\n\r]';
%%Open the text file.
fileID = fopen(waxs_exposures_2015, 'rt'); %changed
...
fclose(fileID);
1 Comment
Steven Lord
on 30 Jan 2016
And if you want to call that function using the name waxsimport it must be contained in a file named waxsimport.m. [If the function name and the file name are different, MATLAB knows the function by the file name.]
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks 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!