1

I am discovering something that is, quite frankly absurd, about MATLAB. It is not possible in a session of MATLAB to open two files or even reference by link two files with the same name, even if in different folders (as would re required by any file system.) In fact it isn't possible to have two identically named files accessible on the MATLAB path without leading to ambiguity when addressing files.

In my quite large solution I have resolved to giving the full name of the file, inclusive of its location in the model, to allow unique file names and not violate this limitation. Up to now this has resolved the issue in a somewhat ugly way. However I have just now discovered a second issue that compounds the first, that being file names are limited to 63 characters. Now I cannot have a name that fits either criteria and be sensible.

It seems the only answer is to choose semi-random file names that fit together into a global menagerie of file names and hope to capture just enough information to be able to identify a file and its purpose.

How else is one to manage large systems?

(Apologies for the tone, I am getting to the end of my tether with Mathworks software.)

J Collins
  • 2,106
  • 1
  • 23
  • 30
  • You can have two or more files which have the same file name on the MATLAB path, but to avoid any issues with one file "shadowing" the other, you should reference them using the absolute path. This ensures the correct file is used each time it is called. I believe you can open two MATLAB files in the editor which have the same name. Probably not possible to do that with Simulink models though. – am304 Feb 14 '14 at 12:49
  • If the files shadowing each other are functions, is it possible to call a function using its full path? I imagine it is at least not graceful. – J Collins Feb 14 '14 at 12:51
  • Not sure... Maybe the following helps: http://www.mathworks.co.uk/help/matlab/matlab_prog/function-precedence-order.html – am304 Feb 14 '14 at 12:58
  • Well, functions shouldn't have the same name to begin with. Unless you want to overload a function, but that usually applies to classes only, so you can select the right function depending on the class of the object you're currently operating on. – scenia Feb 14 '14 at 13:11
  • @scenia it is a bold statement to say no functions should have the same name. Locally yes, two functions with the same name are overloads. Globally however, suggesting one function name should be uniquely related to a specific functionality is not at all correct. In the context of a project you are working on a function name may quite well have a different meaning to the same in my project. If one day the two projects are integrated, the idea that the name collision is a result of one of us choosing the wrong name is simply not right. – J Collins Feb 14 '14 at 14:11
  • If two projects share a function name, it's nearly always a good idea to integrate them as different classes or packages. It's actually even a good idea if they don't necessarily share names. There's more to integrating two projects than just copying the files from one of them into the root directory of the other. If you somehow end up with entirely different functionalties contained within functions of the same name that overload each other, you've done something wrong and should fix the integration. That's not Matlab's fault. It leads to problems in any and every language/compiler/system. – scenia Feb 14 '14 at 14:20
  • @scenia I would have to disagree that it should be difficult to integrate projects. I will have to do some research into packages as this is possibly the solution to a lot of problems so long as Simulink is fully compatible with the notion. – J Collins Feb 14 '14 at 14:25
  • Well, not difficult, but just copying them into one another will break things more often than it will work. To avoid that, you'd need to integrate them via packages. This should solve any problem. I'm not sure if Simulink is fine with that, though, I think this one might actually be nitpicky about same name models. – scenia Feb 14 '14 at 14:32
  • You can take a look at the answers to a [question](http://stackoverflow.com/questions/17727141/how-to-select-a-specific-m-function-when-two-exist) I asked. – m_power Feb 14 '14 at 14:35
  • @scenia I have just done a simple test. MATLAB m files of the same name can be opened and edited together, Simulink models in package folders (with the '+' prefix) cannot. – J Collins Feb 14 '14 at 14:38
  • @m_power, I just tried the 'private' folder idea (with the '@' prefix, still no dice.) – J Collins Feb 14 '14 at 14:38
  • Then Simulink probably just has that limitation due to the way it internally handles open models... I can't test it because I don't have a Simulink license currently, but can you open different projects simultaneously and one model (with gloablly the same name) in each of them? – scenia Feb 14 '14 at 14:42
  • At no point in a session of Simulink can two identically named files be accessed. Whether in one model with links, separate models with links etc. – J Collins Feb 14 '14 at 14:45
  • So your simulink model calls two different script? From where does your simulink model call the script (i.e. which callbacks)? I would like to try to test it with a simple example to reproduce your problem in order to help you. – m_power Feb 14 '14 at 15:13
  • @m_power, the simplest test is to set up a test folder with two subfolders. In each subfolder create a model file with the same name. Now try to open both models in the same session of MATLAB/Simulink, the second one will throw an error telling you a file of that name is already open. To duplicate exactly what I'm trying to do, repeat the above with two library files and try to create a model with library links to an identically named subsystem in each. In this case the model picks up an arbitrary selection of the two models. – J Collins Feb 14 '14 at 15:28
  • I tried that simplest test. I found the same error. There is probably something you should do differently in order to avoid that problem. – m_power Feb 14 '14 at 15:54
  • I know however that you can run the same model in different Matlab instance. For example, I have 8 instance of Matlab that run the same model using the `load_system` command. – m_power Feb 14 '14 at 15:56
  • @m_power I know the two models can be opened in multiple instances, that doesn't help having the two models referenced with a link in one model. If the truest answer is that Simulink just doesn't handle the situation properly, I'm not going to accept that Simulink's poor handling reflects my error as a user. But if there is something else I should be doing, what is that thing? – J Collins Feb 14 '14 at 16:00

2 Answers2

2

That's the drawback of matlab's policy with the path and the "convenience" of putting thousands of files onto it.

If you're talking about "data-files" you're loading (via load) etc., then the solution is to use absolute path names. That's the proper way to do it anyway, imho.

You can use different functions with identical names, there are a few possibilities to do so:

  1. cd into the folder that contains the function you're wanting to call. That puts the function in the current directory to the "top" of the path. Not nice, but it works.
  2. Put different, equally named functions into packages and call them via package1.fun() package2.fun().
  3. Do things in an object-oriented manner. This reduces the raw number of m-files flying around dramatically.
sebastian
  • 9,526
  • 26
  • 54
  • I am primarily working with Simulink models and a subversion repository to manage them. As a result I am trying to create as many model files at possible with interlinking, to make change management possible. Thanks for the info on packages though, I didn't know MATLAB had any concept of name spaces. – J Collins Feb 14 '14 at 14:49
1

From this link, the variable names are indeed limited to 63 characters. Filepaths aren't limited to 63 characters, so I don't see why you are having problems with giving the exact location.

m_power
  • 3,156
  • 5
  • 33
  • 54
  • 1
    He's probably using references to filepaths as variables. Something like `filepath = 'ReallyReallyLongPathNameThatExceeds63CharactersBecauseOfAStrangeFileSystem.m'` – scenia Feb 14 '14 at 13:14
  • Close, I am using function files where the function name matches the file name. The problem right now though is saving a Simulink library file, this is where I discovered the file name size limit. – J Collins Feb 14 '14 at 14:07