How can I do the following with MATLAB Editor

1 visualización (últimos 30 días)
Abhilash Sukumari
Abhilash Sukumari el 14 de Nov. de 2017
Editada: per isakson el 24 de Nov. de 2017
I purchased matlab license 2 days ago and coming from a Python background, language similarities seems to be a smooth transition. However, there are a few things that I find missing at this point in the editor, perhaps someone could advise me on the following points:
  1. How to enable indenting in command window as well just like how we can enable it for the editor.
  2. How does 'path' work? It seems to be defaulted at Documents/MATLAB/. How do you organize and manage your project files from this point on?
  3. How do you configure your matlab code in such a way that for example, I want to reformat my code with whitespace around any operator or a whitespace before any keyword. For example the two lines of code below have an imbalanced whitespace distribution. This irks me. I want a neat code. Is there a way to do this w/o depending on external (3rd party) scripts or functions
%impropper whitespace balance befor/after operator
fidRead= fopen(fileName,'r');
fidWrite =fopen(bufferFile,'w');
%space before keyword
tline = fgetl(fidRead);
%no space before keyword
tline =fgetl(fidRead);
At this point I believe MathWorks is undergoing some technical issue with their servers, one cannot play any video tutorials on their website.
  2 comentarios
Adam
Adam el 14 de Nov. de 2017
The path works just like the windows path. You add whatever locations you want to it and that is where Matlab will look for functions.
Personally I have a MatlabDev folder that is linked to our Mercury/Hg repository and a load of subfolders under this which are mostly added to the path. I don't use the default location in Documents at all. I do, however, leave all other automatically added folders on my path as these are what give access to all the Matlab toolbox and builtin functions.
I add whitespace around operators myself, though like with any editor you can do find and replace stuff on a whole file.
Abhilash Sukumari
Abhilash Sukumari el 15 de Nov. de 2017
I have created something called MATLAB-Projects folder. So this is like a windows explorer? In other words if I want to work on a project "A". I would just browse to MATLAB-Projects/A ?
Then what is SET PATH under HOME ribbon? and what's that 'Add to Path' option when you right click on any folder in command window?
Which one should I follow to work in a given project folder?

Iniciar sesión para comentar.

Respuesta aceptada

per isakson
per isakson el 15 de Nov. de 2017
Editada: per isakson el 24 de Nov. de 2017
  1. Unknown in the Matlab world. Who would want that ;-)
  2. Good question. See What Is the MATLAB Search Path?. Use Packages Create Namespaces even if it requires more planning and typing. It requires a lot of manual work to organize a code base with packages afterwards. I have all my matlab code in subfolders under a folder named, "m", far away from the Matlab installation. And see the blog-post Best Practices – Adapt, then Adopt!
  3. Unknown in the Matlab world. We do that by typing and ask our-self what editor The MathWorks use in-house. The wish-list regarding the editor is long. I use Notepad++ when I "must" have regular expressions and more ... .
  4. Now "video tutorials on their website" are up and running.
"In other words if I want to work on a project "A"."
  1. Create the folder: MATLAB-Projects/A
  2. Add A to the MATLAB Search Path, which can be done in at least four different ways. There is a Set Path GUI under the HOME-ribbon, a command addpath, right click context menu, and more. That is good for the current session. To add it permanently you have to Save Path.
Live Edit might be the first manifestation of Matlabs new editor. See Introduction to the Live Editor
  2 comentarios
Abhilash Sukumari
Abhilash Sukumari el 16 de Nov. de 2017
Editada: Abhilash Sukumari el 16 de Nov. de 2017
  1. Adding MATLAB-Projects/A to the search path (using set path GUI under HOME ribbon) doesn't necessarily mean your script under /A works. Why? Because, if you use >> dir command in your script it will always look for files under the current folder (>> pwd). If it doesn't find what's it looking for it will give up.
  2. The only way I could make it work is to manually browse to MATLAB-Projects/A folder using Current Folder GUI. Then I executed >> pwd and >> dir to verify if all the files required are seen by the script.
This is/was the ambiguity I was talking about, setting your path doesn't necessarily mean your script will run; especially if you are using >> dir to search through files that hold some data. You have to browse to the project folder intended to be used.
per isakson
per isakson el 16 de Nov. de 2017
Editada: per isakson el 16 de Nov. de 2017
  • The main effect of adding a folder to the search path is that Matlab looks in that folder for executable files. See Files and Folders that MATLAB Accesses. Thus, Matlab will find your script in MATLAB-Projects/A.
  • dir is a different story. It doesn't scan the search path. See dir, List folder contents. (Matlab has evolved over a period of forty years. Old features are not easily removed, because that would alienate the users. That explains ... .) In your script use something like sad = dir(glob), where glob is a string, which may include wildcards. IMO: don't make scripts, the behavior of which depends on the value of the current folder.
  • "The only way" No! There are two alternatives. 1) Use a sandbox, make that the current folder, keep all files in that folder. That's good for small experiments. 2) Write the script/function so that it does not depend on the current folder.
  • BTW: For serious programming use functions and classes, not scripts.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by