Hi, I'm writing a MATLAB function that needs to change directories, but I keep getting this error:
Error in GetStations (line 5)
home = cd;
Here is part of my function:
function [Stations,NumStations] = GetStations(NLat,SLat,ELon,WLon)
% This function searches a list of seismic stations for those within the geographic box specified by the input latitudes and longitudes.
home = cd; % Where I'm currently working
% Go to where the seismic station list is saved:
cd('C:\Users\User\Desktop\Research\MATLABFunctions');
StationList = fopen('Stations.txt'); % Open text file
% Read file, skip first line:
AllStations = textscan(StationList, '%s %s %f %f','headerlines', 1);
fclose(StationList); % Close text file
cd(home); % Go back to where I'm currently working

1 件のコメント

Walter Roberson
Walter Roberson 2015 年 7 月 27 日
Please show the complete error message.

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2015 年 7 月 27 日
編集済み: Stephen23 2015 年 7 月 28 日

1 投票

The syntax
home = cd;
is possibly not valid: it is not covered by the online documentation. The best solution is to avoid using cd for this anyway: using cd makes your code slow, fragile and difficult to debug. A faster, neater and much more versatile solution is to pass filepaths instead. So remove all of those cd's, and try this:
my_path = 'C:\Users\User\Desktop\Research\MATLABFunctions';
my_file = 'Stations.txt';
my_full = fullfile(my_path,my_file);
%
fid = fopen(my_full,'rt');
C = textscan(fid, '%s%s%f%f', 'headerlines',1);
fclose(fid);

7 件のコメント

Walter Roberson
Walter Roberson 2015 年 7 月 27 日
home = cd;
is valid syntax. Look in "help cd"
WD = cd returns the current directory as a string.
Image Analyst
Image Analyst 2015 年 7 月 27 日
I think Stephen meant that since "home" is a built in function, you should not assign the folder, the output of cd, to a built in function, thus destroying it. Anyway, when I get the current working directory, I use pwd:
currentFolder = pwd
Actually I never use cd to change folder either (or only in very special rare instances). FAQ: http://matlab.wikia.com/wiki/FAQ#Where_did_my_file_go.3F_The_risks_of_using_the_cd_function.
Walter Roberson
Walter Roberson 2015 年 7 月 27 日
編集済み: Walter Roberson 2015 年 7 月 27 日
Overwriting "home" is not a syntax problem, just a clash of names on a lesser-used routine. I don't recall ever having used home() .
Muthu Annamalai
Muthu Annamalai 2015 年 7 月 27 日
Using cd need not be an entirely bad idea. You may use methods like onCleanup() to restore to original paths in events of incomplete function run and to prevent unclean exits.
Rhiannon Vieceli
Rhiannon Vieceli 2015 年 7 月 28 日
編集済み: Rhiannon Vieceli 2015 年 7 月 28 日
Thank you for all of your responses. I now understand why I should avoid using cd in this manner. However, I'm still having trouble with the function. It doesn't seem to like the first line of code, regardless of what it is. For example:
function [Stations, NumStations] = GetStations(NLat,SLat,ELon,WLon)
% This function searches a list of seismic stations for those within
% the geographic box specified by the input latitudes and longitudes.
x = 3;
myPath = 'C:\Users\User\Desktop\Research\MATLABFunctions';
returns the error:
Error in GetStations (line 5)
x = 3;
Output argument "Stations" (and maybe others) not assigned during
call to "C:\Users\User\Documents\MATLAB\GetStations.m>GetStations".
Stephen23
Stephen23 2015 年 7 月 28 日
編集済み: Stephen23 2015 年 7 月 28 日
The problem is not the first line of code (whatever it may be), but the fact that within your function the outputs Stations and NumStations have not been defined. If you are calling the function with these outputs but they are not defined inside the function then this causes an error to be thrown. One simple solution is to temporarily define some placeholder values at the beginning of the function, and remove them later when you calculate the real values:
Stations = [];
NumStations = [];
Rhiannon Vieceli
Rhiannon Vieceli 2015 年 7 月 29 日
It turned out that my search window was too small and no stations were found. As it turns out, when NumStations is zero, Stations is deleted and causes the error. It's all good now! Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGeographic Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by