I have 2 paths :
C:\controller\functions\verifyModel.m
C:\OGVD\prod\KMLP\controller\controllerStatus.m
verifyModel.m
classdef verifyModel
methods(access=public)
function...
end
end
controllerStatus.m
classdef controllerStatus < verifyModel
.....
end
but when I run controllerStatus.m, I got an error as the class I used isn't in the path
how could I add verifyModel to the path ?
Before usage of controllerStatus use:
addpath('C:\controller\functions\')
Also, you might want to put in in a # folder. These folders are added to the path whenever they are visible, so as they are a subfolder of your current path(pwd).
Or add 'C:\controller\functions\' to your static matlab path, what I do not recommend.
See also this answer.
Related
I have a GUI using a Browe Button to search a file :
function Browse(app, event)
FileName,FilePath ]= uigetfile();
ExPath = fullfile(FilePath, FileName);
app.FileTextArea.Value = ExPath;
end
And i save the file Path in a Text Area.
I have another button that start a matlab script with the file path as parameter and so i would like to accept only a certain type of file (.ctm which is my own type of file) if possible like this :
if file is .ctm
do something
else
print('a .ctm file is needed')
Thanks for helping
There are two things you can do:
Display only the files with a certain extension with uigetfile()
[fileName, dataDir] = uigetfile('*.ctm', 'Select a *.ctm file', yourDefaultPth);
Verify that selected file has a .ctm extension
[data.dir,data.fileName,data.ext] = fileparts(fullfile(dataDir, fileName)); % dataDir and fileName from pt. 1
if strcmp(data.ext, '.ctm')
% do something
else
print('a .ctm file is needed')
end
Keep in mind that neither of the two will verify that the content of the file is the one you're expecting and if someone will manually modify extension of the file, your program will most likely crash. It's good for a start but if you want to do a more reliable check, you should verify that the content of the file is correct, not its extension.
I have a Matlab(R2017b) script that is at path:
path1: C:\ComputeCode\Scr1.m
and a script like:
path2: C:\ComputeCode\OtherFiles\Scr2.m
Scr1.m has a code which does bunch of stuff like below:
%..... Scri.m.....%
..open files and do some processing...
cd(path2)
.. execute Scri2.m and do some processing ...
The issue is that if there is an error in Scr2.m, the control
does not come back to path1, but stays in path2.
How do I add some code at start of Scr1.m, so that whenever any exception/error
pccurs in matlab, it always default to path1 for execution.
The proper way to accomplish this would be to utilize absolute file paths rather than relative ones so you don't have to worry about having to cd into a directory for proper functionality.
Other methods include:
onCleanup, which executes code when the output object is destroyed. Note that this will require you to make Scr1 a function in order to work with minimal additional effort.
For example, we have SOcode.m:
function SOcode
home = pwd; % Store base directory
cleanupObj = onCleanup(#()cd(home));
cd(fullfile('./testfldr')) % Use fullfile for platform independence
asdf
end
And ./asdf.m, which contains an error:
disp(a)
Upon execution of SOcode, you'll receive an error:
>> SOcode
Undefined function or variable 'a'.
Error in asdf (line 1)
disp(a)
Error in SOcode (line 6)
asdf
But be returned back to the base directory.
Alternatively, you can use a try/catch to catch the exception and return to the home directory before rethrowing the error. This approach does not require Scr1 to be a function.
For example, we now have SOcode.m:
home = pwd;
cd(fullfile('./testfldr'))
try
asdf
catch e
cd(home)
rethrow(e)
end
With the same ./asdf.m, for the same result.
Use try and catch
%..... Scri.m.....% ..open files and do some processing...
try
cd(path2)
.. execute Scri2.m and do some processing ...
catch
cd(path1)
break
end
or something similar...
Trying to create my first class in MATLAB but obviously am missing something.
So here is my class below.
classdef MyBank
properties
Balance;
CustName;
end
methods
function BA = MyBank()
BA.Balance = 0;
BA.CustName = 'Mr Blogs'
end
end
end
In the same path I have a m file. In this file I try to create an object from my class like so,
bank = MyBank;
I get the error message 'undefined function or variabel 'MyBank'? Not sure what I'm missing as the examples I have seen appear to do the same thing?
Also when typing BA in my constructor should there be any intellisense? Find it quite painful coding in Matlab.
Matlab doesn't understand ".
You shuold use BA.CustName = 'Mr Blogs'
Are you using Matlab or Octave? Octave understands ", but last time I checked classdef is not working.
To find the constructor with "intellisense", you should type "My" and then press tab. At least for me this works.
If this doesn't work for you, check that your file is named MyBank.m and double check if it is in your current working folder. Open the file in your edior window and execute it by pressing F5. Then a dialog should pop up, if your in another working directory.
I have 2 paths :
C:\controller\functions\verifyModel.m
C:\OGVD\prod\KMLP\controller\controllerStatus.m
verifyModel.m
classdef verifyModel
methods(access=public)
function...
end
end
controllerStatus.m
classdef controllerStatus < verifyModel
.....
end
but when I run controllerStatus.m, I got an error as the class I used isn't in the path
how could I add verifyModel to the path ?
Before usage of controllerStatus use:
addpath('C:\controller\functions\')
Also, you might want to put in in a # folder. These folders are added to the path whenever they are visible, so as they are a subfolder of your current path(pwd).
Or add 'C:\controller\functions\' to your static matlab path, what I do not recommend.
See also this answer.
I would like to be able to run a function from the directory where it is defined.
Let's say this is my folder structure:
./matlab
./matlab/functions1
./matlab/functions2
and I have all directories in my MATLAB path, so I am able to call the functions that are in these directories.
Let's say my function "func" resides in 'matlab/functions1'. My function contains command
csvwrite('data.csv', data(:));
Now, if I call "func" from ./matlab, 'data.csv' gets created in ./matlab. If I call it from ./matlab/functions2, it will get created in that directory. But I would like for the function to write 'data.csv' always in the directory, where the function is defined (./matlab/functions1), no matter what my current directory is. How can I achieve that?
mfilename called from 'inside' a function returns the function path and name.
fullPath = mfilename('fullpath');
pathString = fileparts(fullPath);
dataPath = [ pathString filesep 'data.csv'];
csvwrite(dataPath, data(:));
In addition to what #zellus suggested, you can use functions to get information on a specific function, regardless of any m file being executed at the same moment. You set the function of interest by giving functions the function handle:
funInfo = functions(#func);
fullPath = funInfo.file;