How to invoke a MATLAB script in runtime by its name - matlab

Suppose I have 3 m-codes:
code1.m code2.m code3.m
and I want a code for MATLAB to "draw them together" in the sense that when we run the program, we are prompted with, say, "enter code:", then the user types in say "code3" and then code3.m is run.
I am pretty sure there is a simple code to do that, though I can't remember it.

There are two portions to this question, the first of which is getting user input:
Matlab allows you to request user input as shown in this tutorial: http://www.mathworks.com/help/techdoc/ref/input.html
strResponse = input(prompt, 's')
Part two is simply loading the file and executing it, as described by #MetalRain
http://www.mathworks.com/help/techdoc/ref/eval.html
eval(['load code' strResponse '.m'])
Noting that matlab perform string concatanation on the vector for you, so the result for the input of strResponse = 1 is 'load code1.m'

run or eval can do it. You get the name of the file from input.
A (maybe) less flexible but safer method is to use the graphical version of input named inputdlg.

Related

how to show the linearization result in Dymola?

I tried to use the linearization function in Dymola, but it seems when the result's dimension is large, Dymola won't show the result.
My question is:
How could I print the result or where to find it?
What you can do, is assign the result to a variable. This can be done using the Outputs group as shown in the screenshot below. If you e.g. enter "sys" in the field for ss, you will get a record sys, in which you can access the matrices/vectors by typing sys.A, sys.B etc., which I've tested for a system of size 200x200. Typing this into the command line will display the content. Of course this record not only for outputting it, but also for post-processing.
The only thing this actually does, is modify the call from Modelica_LinearSystems2.ModelAnalysis.Linearize("ModelName") to sys=Modelica_LinearSystems2.ModelAnalysis.Linearize("ModelName"), so it can be done in the Commands window as well.
Call the function from the command line and capture the output. Then you can do with it what ever you want.
Everything you find in the Linear Analysis toolbar is part of the library Modelica_LinearSystems2. The Linearize item in this menu calls the function
Modelica_LinearSystems2.ModelAnalysis.Linearize("<your-model>")
which is also printed to the command line. The function returns the operator record Modelica_LinearSystems2.StateSpace, which contains all the info you are interested in. The default behavior of Dymola is to call the String method of this operator record and print it to the command line. If you look at the source code of Modelica_LinearSystems2.StateSpace.'String' you can see this at the start of the algorithm section:
// If system is too large, do not print the matrices
if size(ss.A,1) > 50 or size(ss.B, 2) > 50 or size(ss.C, 1) > 50 then
...
On the command line you can capture the operator record in a variable like this:
stateSpace = Modelica_LinearSystems2.ModelAnalysis.Linearize("<your-model>");
And then access the values on the command line via
stateSpace.A
stateSpace.B
stateSpace.C
stateSpace.D
For a nice html report you can also pass the operator record to one of the analysis functions:
Modelica_LinearSystems2.StateSpace.Analysis.analysis2.printSystem(stateSpace)
This creates the file systemAnalysis.html in your working directory, containing a nice visual presentation of your system.

matlab 'evidence removio': delete last shown command

I would like to remove whatever I just typed - and the last shown command - from the matlab console display. Needless to say, this would be ideal for pranksters (but this is of course strictly for academic purposes only). This is as far as I have gotten (based on this related answer):
hist = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory; %get history
last = strjoin(cell(hist(end-2:end)),' '); %convert history to string
fprintf(repmat('\b',1,numel(last))); %replace characters of string with whitespace
However I can only access the last typed command (through the command history) - not the last displayed command (which would be ideal). Any ideas how to solve this?
Disclaimer: I don't advise doing this.
The MATLAB CommandWindow contents are stored as a CmdWinDocument, which is an extension of the Java PlainDocument type, and an interface of the Document type. The current window can be accessed using the command:
com.mathworks.mde.cmdwin.CmdWinDocument.getInstance
In theory, you should be able to remove text from the command window using something like:
doc = com.mathworks.mde.cmdwin.CmdWinDocument.getInstance
endpos = doc.getEndPosition
doc.remove(endpos-10,10)
which would, in theory, remove the last 10 character from the document. You may have to call the removeUpdate function as well. Obviously problems will be caused by the fact that these commands will be appended to the document during this process. I have not tested this, and you're likely to cause problems with internally stored offsets within the CmdWinDocument class, so use at your own risk.

*.m file requires external *.mat file for input. Not sure how to implement

Long time reader first time poster!
I am having the pleasure of learning more advanced MatLab by going through my Professors code, which is proving to be a little difficult.
At the moment we have a GUI which runs some functions on external data and produces some graphs as an output. I would however like to run the function by itself without having to use the GUI.
The function heading looks like this...
function info = fn_2d(exp_data, data, options)
Where exp_data is a structure *.m file, data is a function stored in the m file itself and options is, well, I'm not sure, but the inputs come from the GUI (such as coordinates of mouse clicks etc).
I'm a little out of my depth but I am going through the code line by line.
Could anyone help me with how to read the exp_data directly from the directory. I tried to add this and excluded exp_data from the input arguments for the function, but it didn't work.
exp_data_file = ('Example.mat');
exp_data = matfile(exp_data_file);
Again, apologies for being quite so out of my depth.
R

Script for running (testing) another matlab script?

I need to create a matlab mfile that will run another matlab file with default values given in a txt file. It's ment to be useful for testing programs, so that user may specify values in a txt files and instead of inputing values every time he starts the program, my script will give the program default values and user will only see the result.
My idea is to load tested file into a variable, change 'variable=input('...');' for variable = default_variable;, save it to tmp file, execute, and than delete tmp file. Is this going to do the job?
I have only two problems:
1) How to eliminate the problem of duplicated variable names - i mean this must work for all scripts, i don't know the names of variables used in tested script.
2) As I wrote before - is this going to work fine? Or maybe I missed a easier way to do it - for example maybe I don't have to create a tmp file?
I really need your help!
Thanks in advance!
If the person who has to edit the default values has access to Matlab, I would recommend saveing the values in a mat file and loading them when needed. Otherwise you could just write a smalls cript that contains the assignment to certain variables, but make sure to keep this small. For example:
maxRuns = 100;
clusters = 12;
So much for setting up the defaults. Regarding the process my main advice is to wrap the thing that you want to test into a function. This way variables used in the code to call the 'script' will not interfere as a function gets its own separate workspace. Check doc function if you are not familiar with them.

Is there a way to attach a file (.txt) in calc open office programmatically (using macro)?

I have a .txt file that I need to attach in a column of my sheet, and i have the path to this file.
So I need to read this path and attach the file in another column programmatically. Is there a way to do it?
thanks in advance.
Indeed there is! And using by using macros it is quite easy to do.
Enabling macros
Go to the Tools > Options menu and click on the Security section under OpenOffice.org. Once there, click the Macro Security button. Now on the Security Level Tab, make sure that your settings will allow you to run Macros.
My settings are on low because I'm the author of all the macros I run, if you are not sure that this will be your case you might want to use a higher setting.
Note: Be careful, if you are unlucky or live in the 90's an evil macro can cause serious damage!
Creating a new macro
Now that you can run them, you must create a new macro. OpenOffice accepts a wide range of languages including Python, but since you didn't specified any I'll use OO's version of basic here.
Go to Tools > Macros > Organize Macros > OpenOffice.org Basic, and once there add a new module under your file's tree. Give it a meaningful name.
The actual macro
Once you create a new module the editor screen will pop up, write this code below:
Sub DataFromFile
Dim FileNo As Integer
Dim CurrentLine As String
Dim File As String
Dim Msg as String
Dim I as Integer
' Get the filename from the cell, in this case B1.
currentSheet=ThisComponent.CurrentController.ActiveSheet
fileName = currentSheet.getCellRangeByName("B1").getString
' Create a new file handler and open it for reading
FileNo = FreeFile
Open fileName For Input As #FileNo
I = 0
' Read file until EOF is reached
Do While not eof(FileNo)
' Read line
Line Input #FileNo, CurrentLine
' Define the range to put the data in as A4:A999 '
curentCell = currentSheet.getCellRangeByName("A4:A999").getCellByPosition(0,I)
' Select the I-th cell on the defined range and put a line of the file there
curentCell.String = CurrentLine
'Increase I by one
I = I + 1
Loop
Close #FileNo
End Sub
To test it, just create a text file and put something in it, then put the path to it on cell B1 and run the macro. You can run the macro in many ways, for test purposes just use the Run button on the same window that you used to create the module. This is the expected result:
Note: If you are unfamiliar with linux, don't be intimidated by that file path, it's just how they are on linux. This would just work the same with windows and it's file path structure.
Further improving the macro
I wrote the code above with the goal of making it as easy to understand as possible, therefore the macro have plenty of room for improvement, such as:
Being able to show the data retrieved on multiple columns/A single column/Something else
Once you have retrieved the data from the file, you can display it on your spreadsheet in nearly anyway you want it. Let me know if the way you initially intended was not addressed and I will edit the answer.
Having to re-run the macro every time you want the data updated.
This is easily fixed. There are many ways to automatize the macro execution, the one I'm most familiar with consists on making it run on a loop in conjunction with a delay of, say, 5 seconds and making it start as soon as the file loads.
Sub Main
Do While True
DataFromFile()
Wait(5000)
Loop
End Sub
And from now on you should call the Main sub instead of the DataFromFile.
To make the macro run at start-up go to Tools > Customize on the Events tab and select Open Document from the list then click on the Macro button. On the dialog to select the macro, pick Main. Now close the document, reopen it, and voila!
Using Cell Ranges
It's easier to keep your code and make changes to it if you name the cell ranges and use their names instead of their absolute address. To name a range (or a single cell) you must first select it then click on Data > Define Range to give it a name, for example B1 could be called 'FilePath' and A4:A999 could be called 'DataRange'. This way if you ever need to change them, you don't have to change the macro, just the defined range name.
Don't forget to update the code to look for the range instead of the address, for example, this bit of code:
getCellRangeByName("A4:A999")
would be rewritten to
getCellRangeByName("DataRange")
Error checking
It is a good idea to check and deal with error or unexpected events. What if the file doesn't exists? What if it is bigger than the defined range?
Further reading
Official reference regarding files for OpenOffice Basic macros.
A guide on different ways to run a macro
A great introduction to macro programming