matlab 'evidence removio': delete last shown command - matlab

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.

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.

How to change obtained format

I've got a problem changing my code result. The main problem is when I run the code, it works very well, and the final results are accurate. However, as you can see it's like a division of 2 big numbers. Please help me out how to change the formation of the results. I have to say that I've already used **format command ** and didn't get anything.
the result I want is something like :
sigma=156.45e+6;
Thanks.
The format command isn't what you need here I think.
If these values were generated using the symbolic toolbox, then they tend to remain as whole fractions, and in order to change this, you simply need to run the following code either in your script or in the command window:
sympref('FloatingPointOutput',true);
This will produce the values you are looking for.
Alternatively you can cast the values you have to a double using the following code:
ans = double(ans);
sigma1 = double(sigma1);
sigma2 = double(sigma2);
You must have set the command window format to long some time ago as this is not the usual behavior.
You can change this simply by typing to the command window.
The defaults are:
format shortEng % number representation
format compact % line spacing
BTW, you can get your current setting with this command
get(0,'Format')
However, changing the format only applied to your current session (until you close MATLAB). Therefore it is odd that you ask. If it persists to be changed, someone must have fiddled with the preference
The specified format applies only to the current MATLAB session. To maintain a format across sessions, choose a Numeric format or Numeric display option in the Command Window Preferences.

Copy name of object in variable explorer

I frequently find myself examining deeply nested data in the variable explorer, e.g.:
objectName.structArray1(5).structArray2(3).structArray3(7).doubleArray(4)
In order to be descriptive, the variable names are often long. I often want to use some of the data I'm looking at in Matlab expression, composed at the command line. So I end up typing the lengthy series of variable names and indexes. Autocompletion helps, but not much, especially since my variable names share many substrings.
It would be a lifesaver if I could copy into the clipboard the entire expression corresponding to the data being examined in the variable viewer. I haven't yet found a way to do this (the most obvious way being to right-click the tab for the data being examined). So I'm not sure if this functionality exists. Can anyone confirm or deny (hopefully the former) whether this functionality exists? If it does, how is it done?
As an example, suppose you had a class file myClass.m in the current working directory:
% myClass.m
%----------
classdef myClass
properties
structArray1
end % properties
end % class
Now suppose you issued the following commands:
objectName = myClass
objectName.structArray1(5).structArray2(3).structArray3(7).doubleArray(1:3)=rand(1,3)
openvar('objectName.structArray1(5).structArray2(3).structArray3(7).doubleArray')
You are now examining a slew of data within a deeply nested data structure. Normally, the data would have been the result of computation other than the rand statement above, and I would have browsed to it manually rather than using the openvar statement above. So I would not normally have readily available the text for the expression
objectName.structArray1(5).structArray2(3).structArray3(7).doubleArray
I have to manually type it in at the command line if I want to use it in a Matlab expression for further computation. It'd be so great if I could somehow point to the tab for that data in the variable explorer and somehow have the expression for the data copied to the clipboard. That way, I can paste it to the command line.
AFTERNOTE:
If there's no way to do this, then as an alternative to manually typing in the whole expression above, is there a way to access the corresponding data object (or a copy thereof) programmatically through the variable explorer window object? This assumes, of course, that the variable explorer is itself a data object as well, through which properties can be accessed. If so, maybe it has a property (perhaps deeply nested) that represents the expression for data in the tab that currently has the focus. If so, I can write a function to retrieve the corresponding data object.
I found that if I undock a tab from the Variables editor, I can select the variable name by double-clicking the variable name in the tab.
Sorry for the ambiguity in wording, but "tab" use to mean the protrusion in the data sheet for displaying the name of the data sheet. Nowadays, "tab" means the whole data sheet. In the first sentence above, I mean the protrusion, which unfortunately doesn't have a distinct name these days (at least none of which I'm aware).
After copying and pasting the variable name from the protrusion, the tab can be docked, which seems to put it back into its original place.

Retrieve past results

Is it possible to retrieve previous results in the command window? For example i had some matrices and vectors in the command window and after an error the matlab was closed . Is it possible to retrieve them?
Thanks
No guarantees about what happens after a crash, but in general, use the command diary('filename.txt') to append all text in the CommandWindow -- not the Command History window -- to that text file.
(I dunno why MathWorks doesn't call the "CommandWindow" what it is, i.e. the Console, but there you are.)

Emacs: Is there a way to generate a skeleton ChangeLog from diff?

I'd like to partly automate creation of GNU-style ChangeLog entries when working with source code in version control. The add-changelog-entry-other-window works with one file at a time and you have to visit the file to use it.
What I'd like to see instead is to have some command that would take an output of diff -u -p (or have integration with VC modes so it could process svn diff etc) and to create all the skeleton entries at once.
For example, if svn status shows
D file1.c
M file2.c
A file3.c
the command would create
2009-09-05 My Name <my.email>
* file1.c: Removed.
* file2.c: WRITE YOUR CHANGES HERE
* file3.c: New.
Better yet, if it could parse the changed files in some languages to an extent so it could offer:
* file2.c (new_function): New function.
(deleted_function): Removed.
(changed_function): WRITE YOUR CHANGES HERE
I have found this feature in Emacs manual, but I don't see how I could apply it here.
Any suggestions? Thanks.
EDIT: One answer suggested vc-update-change-log. Unfortunately it only supports CVS and it creates ChangeLog entries by querying the already-commited VC logs. Thus even if it supported svn and others, it would be impossible to commit the changes and the ChangeLog in the same commit.
EDIT2: Apparently add-changelog-entry-other-window (C-x 4 a) works not only from visited file but from diff hunk involving that file too. (Source) This is almost what I am looking for. This together with elisp loop to iterate through all hunks should solve it.
There is a function vc-update-change-log that automatically generates change log entries from the version control log entries.
diff-add-change-log-entries-other-window is documented to do exactly what you mentioned in EDIT2:
diff-add-change-log-entries-other-window is an interactive compiled
Lisp function in `diff-mode.el'.
(diff-add-change-log-entries-other-window)
Iterate through the current diff and create ChangeLog entries.
I.e. like `add-change-log-entry-other-window' but applied to all hunks.
Unfortunately, it doesn't work very well for, say, new files: it doesn't even include the filenames of such files in the skeletal changelog entry.
You might have better luck with gcc's mklog script, which you can get from http://gcc.gnu.org/viewcvs/gcc/trunk/contrib/mklog.
I don't know of a function that does this, but it should be easy to implement. Basically, you want to
get the changed files
for each file, call add-change-log
"Find change log file, and add an entry for today and an item for this file.
Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
name and email (stored in `add-log-full-name' and `add-log-mailing-address').
Second arg FILE-NAME is file name of the change log.
If nil, use the value of `change-log-default-name'.
Third arg OTHER-WINDOW non-nil means visit in other window.
Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
never append to an existing entry. Option `add-log-keep-changes-together'
otherwise affects whether a new entry is created.
Option `add-log-always-start-new-record' non-nil means always create a
new record, even when the last record was made on the same date and by
the same person.
The change log file can start with a copyright notice and a copying
permission notice. The first blank line indicates the end of these
notices.
Today's date is calculated according to `add-log-time-zone-rule' if
non-nil, otherwise in local time."
so the magic code is going to look something like
(apply 'make-magic-change-log-entry changed-files-list)
and make-magic-change-log-entry simply curries the add-change-log function so that the only argument is file-name — you set the other ones.
I've written a function to do something similar to what you were talking about. You can get the code at http://www.emacswiki.org/emacs/log-edit-fill